File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes The best way to use Exceptions in catch block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "The best way to use Exceptions in catch block?" Watch "The best way to use Exceptions in catch block?" New topic
Author

The best way to use Exceptions in catch block?

Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 202

Hi all Ranchers,

I have a query for the Exception.

What is the best way to use of the Exception?

means if we are define a particular Exception in a catch block like
catch(SQLException sl){
sl.printStatckTrace();}
catch(ClassNotFoundException ce){
ce.printStatckTrace();}
catch(IOException ex){
ex.printStatckTrace();}

and in the last
catch(Exception e){
e.printStatckTrace();}


Is this wrong way to use catch(Exception e){e.printStatckTrace();} after a specified exception.

Is this wrong then correct me.

and if we are using only for all type of exception then is it sufficient
catch(Exception e){
e.printStatckTrace();}



Thanks in advanced!!.
Rajesh Pattabi
Greenhorn

Joined: Apr 03, 2012
Posts: 9
hi Rd,

its good practice to seperate all possible cases in seperate catch block(try to cover all types of exception don't use Exception class). and instead of using printStatckTrace()
use Logger class to print your exception in console.
and don't forget to use finally for query to close your connection, resultset, statement objects.
Rd Dari
Ranch Hand

Joined: Feb 22, 2010
Posts: 202

Hi Rajesh,

Its a good answer which I wanted.

Thanks for a early and good reply
Rajesh Pattabi
Greenhorn

Joined: Apr 03, 2012
Posts: 9
Welcome Rd
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The best way to use Exceptions in catch block?
 
Similar Threads
Nullpointer Exception
Validating a Number
Exception doubt
Static final blank variable
Throwable and Exception