Just try to compile the above code... you will get the answer...
Rajendra Prakash
Ranch Hand
Joined: Sep 10, 2009
Posts: 293
posted
0
Ya i tried . It throws " exception java.sql.exception has already been caught. If sqlexception caught in catch(Exception e) block then why catch(sqlexception)
It throws " exception java.sql.exception has already been caught.
No, it doesn't throw anything. It's a compile error.
If sqlexception caught in catch(Exception e) block then why catch(sqlexception)
SQLException extends Exception, so if you catch Exception first, you'll never be able to reach the SQLException catch block. However, if you place the SQLException catch block before the Exception one, you'll be able to catch SQLException specifically, and catch all other Exceptions in the other catch block. You always have to write multiple catch blocks from most specific, to most general.
Deepak Mahalingam
Greenhorn
Joined: Jul 09, 2010
Posts: 12
posted
0
yes, not only for SQLException, but for any exception, this holds true. The catch(Exception e) must be last in the series of catch blocks that you might have.