This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android 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: 206

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

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
 
 
subject: The best way to use Exceptions in catch block?
 
Similar Threads
Throwable and Exception
Nullpointer Exception
Exception doubt
Validating a Number
Static final blank variable