Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching throwable and handling specific exceptions

Tags:

Ok I know catching throwable is not a good idea:

    try {          // Some code     } catch(Throwable e) { // Not cool!         // handle the exception     } 

But recently I was reading through an open sourced code and I saw this interesting (at least to me) piece of code:

    try {         // Some Code     } catch (Throwable ex){         response = handleException(ex, resource);     }      private handleException(Throwable t, String resource) {         if (t instanceof SQLEXception) {                // Some code         } else if (t instanceof IllegalArgumentException) {                //some code         } //so on and so forth     } 

This doesn't seem to be that bad? What is wrong with this approach?

like image 927
rhel.user Avatar asked Jul 23 '15 10:07

rhel.user


People also ask

How do you catch a throwable exception?

Catching Throwable This means that the calling code is reacting to recoverable and irrecoverable situations in the same way. The general rule in handling exceptions is that the try-catch block must be as specific as possible in catching exceptions. That is, a catch-all scenario must be avoided.

How do you catch an instance of a specific exception type?

Catching Specific Exceptions in PythonA try clause can have any number of except clauses to handle different exceptions, however, only one will be executed in case an exception occurs. We can use a tuple of values to specify multiple exceptions in an except clause.

Should I catch throwable or exception?

Don't Catch Throwable Throwable is the superclass of all exceptions and errors. You can use it in a catch clause, but you should never do it! If you use Throwable in a catch clause, it will not only catch all exceptions; it will also catch all errors.

Does throwable catch error?

Context for this answer: Throwable includes both Error and Exception as subclasses, so first try/catch is inclusive of second but usually over-broad. It also includes user-defined direct subclasses of Throwable and instances of Throwable itself.


1 Answers

There are various reasons why you should not catch a Throwable. First of all is, that Throwable includes Errors - and there's normally not much an application can do if one of these appears. Also Throwable reduces your chances of finding out, WHAT has happened. All you get is "something bad has happened" - which might be a catastrophe or just a nuisance.

The other aproach is better but of course I still would not catch Throwable, but try to catch more specific Exceptions, if possible at all. Otherwise you are catching everything and then try to sort out which kind of bad thing happened. Your example could be written as...

try {     ... } catch (SQLEXception ex){     response = ... ; } catch (IllegalArgumentException ex){     response = ...; } 

...which would reduce the amount of if ( ... instanceof ... ) blocks (which are only needed because the author first decided to catch everything in one big bucket). It something actually throws Throwable, then you don't have much choice, of course.

like image 129
Florian Schaetz Avatar answered Sep 20 '22 13:09

Florian Schaetz