java - Catching throwable and handling specific exceptions -


ok know catching throwable not idea:

    try {          // code     } catch(throwable e) { // not cool!         // handle exception     } 

but reading through open sourced code , saw interesting (at least me) piece of code:

    try {         // code     } catch (throwable ex){         response = handleexception(ex, resource);     }      private handleexception(throwable t, string resource) {         if (t instanceof sqlexception) {                // code         } else if (t instanceof illegalargumentexception) {                //some code         } //so on , forth     } 

this doesn't seem bad? wrong approach?

there various reasons why should not catch throwable. first of is, throwable includes errors - , there's not application can if 1 of these appears. throwable reduces chances of finding out, has happened. "something bad has happened" - might catastrophe or nuisance.

the other aproach better of course still not catch throwable, try catch more specific exceptions, if possible @ all. otherwise catching , try sort out kind of bad thing happened. example written as...

try {     ... } catch (sqlexception ex){     response = ... ; } catch (illegalargumentexception ex){     response = ...; } 

...which reduce amount of if ( ... instanceof ... ) blocks (which needed because author first decided catch in 1 big bucket). throws throwable, don't have choice, of course.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -