Java – Evaluating Exception Handling Practices

exception handlingexceptionsjavajson

I'm working on a project that includes a lot of creating/manipulating and reading JSONObjects and arrays but not in a systematic way. So there is JSON code everywhere.
It is ok for me except that every time I work on a JSONObject I got to handle JSONException.
So I created a class that extends JSONObject and Override the put/get methods in it and handled JSONExceptions inside this class.
It made my code way much clearer and I believe it is more than enough for my case.

What do you think?

Best Answer

I created a class that extends JSONObject and Override the put/get methods in it and handled JSONExceptions inside this class.

This may be perfectly fine or perfectly sucky depending on how you handled the exception inside that class. When (when, not if) an exception arises, what happens? Perhaps there is some central error handler that you're using? How do the users of your JSONObject subclass know if something went awry?

Or do you have a guarantee that things simply can't go awry? If yes, then you should still have some sort of assertion mechanism to catch the impossible.

Exceptions and errors can be handled in myriad of ways. Most of the ways are all right as long as things crash early, with as specific error message as possible, and won't just fail silently later on.

Related Topic