Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try Catch Block

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In try catch block
try{}
catch(Exception e)
{}
catch(SQLException e)
{}

If Sql exception occurs which catch block gets execute.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main question is : does it compile ? Did you try it ?
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajendra,

Just try to compile the above code... you will get the answer...

 
Rajendra Prakash
Ranch Hand
Posts: 293
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes ... Exception is the big daddy ... the master which can catch any other exception as all the exceptions extends Exception
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are actually two things you can catch after Exception: Error and Throwable. But again, Error must be caught* before Throwable.

* Actually, catching any Error is usually a bad idea. They are errors because usually they are really unrecoverable.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Mahalingam wrote: . . . catch(Exception e) must be last in the series of catch blocks that you might have.
Unless you are brave enough to use catch(Throwable), which is hardly ever necessary.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic