• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The best way to use Exceptions in catch block?

 
Rd Dari
Ranch Hand
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 212
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajesh,

Its a good answer which I wanted.

Thanks for a early and good reply
 
Rajesh Pattabi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Rd
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic