2

I have a method which calls several other methods. However these methods and other operations (like navigating array or working with null pointers) can throw runtime exceptions which are not explicit.

One could make use of a try{...}catch(Exception e){...} block to make sure that no wild exception crosses the barrier and my method does not throw any exception which isn't explicit declared in its signature.

However this solution is ugly and prone to bugs since I may not want to catch everything or I may want to provide some specific behavior given a specific error situation.

So, is there a way for Eclipse to show me something like "careful, your method says that it throws IllegalArgumentException and JSONException, but you are missing some runtime exceptions that you can catch internally or declare that you throw them as well"? So far Eclipse only shows me which Exceptions I really must declare in the method's signature, but I also wanna be forced to declare the (Unchecked) runtime exceptions.

Ps.: This is not a duplicate of How can I know what exceptions can be thrown from a method? since the best solution there is to use a catch-all block.

4
  • You don't want to catch all Exceptions which is reasonable, but you may wish to catch all RuntimeExceptions which is not unreasonable. It's analogous to what you want to do which is have control over these unchecked problems creeping out. In my experience I usually have just seen (catch Exception) though, so not sure how ugly it is anymore.
    – Kon
    May 27, 2015 at 15:06
  • catch(Exception e) usually gives sonar violations (of course this depends on your configuration). Sonar is a static code analysis tool which combines results from several other tools.
    – Timo
    May 27, 2015 at 15:08
  • If I get an exception concerning lack of memory I may want to actuate in a given way for example. So I must know that it can be thrown within that method in order to handle it in that context. A catch-all block cannot tell me which exceptions should I expect in that method, and I also want to avoid filling my code with unnecessary catch SpecificException for the sake of not knowing where it might be thrown.
    – PedroD
    May 27, 2015 at 15:09
  • 3
    Not sure if this is still actual, but I learned a phrase somewhere Throw early, catch late. This means that you should catch the exception as late as possible. E.g. if you're handling a message and in some deep down calculations an exception gets thrown, you could discard the whole message at the point of entrance. (Note that in my example you might have to revert changes you made before the exception got thrown!) Also, don't catch an exception at a place where you cannot properly deal with it.
    – Timo
    May 27, 2015 at 15:11

2 Answers 2

2

Short answer is: you can't.

That is the nature of RuntimeException; they are not "checked" exceptions; therefore you are not enforced to add them to your throws clause.

The important thing is that on some level your code understands that RuntimeExceptions might occur; and deals with them in the correct way.

Example: assume you are working on "providers" that should process requests that are coming from a restful API.

There are now two types of problems:

A) those that you "expect" and where you give fine granular feedback on. For example your API spec might list many 400.xxx, 409.xxx, ... answers for various problems. It makes sense to use "checked" exceptions for these things (the code throwing an "ApiException" already knows that this error should show up as 409.xxx to the user of the restful API).

B) Then there are those that you "do not expect"; basically those are reflecting "bugs" in your code; like "he, this argument should not be null"; "he, this 'key' is unknown". For those ... just throw RuntimeExceptions. But make sure that your top layer catches them; and for example turns them into a 500.xxx answer to the user ("some internal error happened, here is the stacktrace; send us a bug report)

2

Your request has an interesting twist. You want to catch all exceptions that the invoked methods may throw, but you don’t want to use catch(Exception ex){ … } because that would imply that … you are catching all exceptions.

Let’s ask the other way round: which exception do you want to exclude from the catch clause? If you want to catch all checked exceptions as well as all runtime exceptions, you exclude Errors and those runtime exception which may not occur. Well, that’s what catch(Exception ex){ … }already does. It doesn’t catch Errors and you don’t need to bother about exceptions that are impossible to occur.

That said, there is a reason why you shouldn’t catch RuntimeExceptions. They are there to signal conditions that normally shouldn’t occur even if their impossibility can’t be proven at compile time.

If you want your IDE to collect all runtime exceptions that are in principle possible, consider

  • Every access to an object or array may cause a NullPointerException
  • Every array access may cause an ArrayIndexOutOfBoundsException or NegativeArraySizeException
  • Every array write access may cause an ArrayStoreException
  • Every integer division may cause an ArithmeticException
  • Every runtime type cast, including those hidden by Generics may produce a ClassCastException

etc. And these are only the exceptions associated with language constructs, not containing those possibly produced by library methods.

The set of possible exceptions will soon explode if you really ask a tool to collect them all for a particular piece of code and all invoked methods.

You would need an insanely large list of exception types in your catch clause and that only to hide the fact that you are actually attempting to catch all exceptions (as catch(Exception ex) would immediately make apparent)…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.