File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Try Catch Block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try Catch Block" Watch "Try Catch Block" New topic
Author

Try Catch Block

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
In try catch block
try{}
catch(Exception e)
{}
catch(SQLException e)
{}

If Sql exception occurs which catch block gets execute.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The main question is : does it compile ? Did you try it ?


[My Blog]
All roads lead to JavaRanch
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Hi Rajendra,

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

Rajendra Prakash
Ranch Hand

Joined: Sep 10, 2009
Posts: 293
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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
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

Joined: Jun 08, 2010
Posts: 384

yes ... Exception is the big daddy ... the master which can catch any other exception as all the exceptions extends Exception


http://plainoldjavaobject.blogspot.in
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38481
    
  23
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Try Catch Block