aspose file tools*
The moose likes JDBC and the fly likes Catching a SQLException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Catching a SQLException" Watch "Catching a SQLException" New topic
Author

Catching a SQLException

Grant Morris
Greenhorn

Joined: Aug 08, 2001
Posts: 8
I have a question about catching a SQLException in an object of type Exception. I'm catching all my exceptions in objects of type Exception; however, I want to check to see if the thrown exception is a SQLException. If the thrown exception is a SQLException, I want to check the sql error code and return an appropriate message. If the thrown exception is not a SQLException, just exit.

Thanks for the help.
Sakti Singh
Greenhorn

Joined: Dec 13, 2000
Posts: 16
if more than one exception raised by a piece of code you can specify more than one catch clauses , where each catch clause will catch a diff type of exception.
for eg.
try {
// your code
}
catch(SQLException se) { //-------1
se.printStackTrace();
}
catch(Exception e) { //----------2
e.printStackTrace();
}
Note: Exception class is super class of all other exceptios..hence should be caught last(last catch block)....
if u change the order of catch blocks to 2--1 , then 1 block will never be executed as 2 is capable of catching all the exceptions
Hope this will solve ur problem
thanks
Sakti
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can also check if your exception object is an instance of SQLException.
ie Catch(Exception e)
if (e instanceof SQLException)
do whatever...

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
Not like that bosun,
try {
// your code
}
catch(SQLException se) { //-------1
//this block will get execute only if ur code throws SQL exception
//there is no need to use instanceof operator
//just write ur SQL exception related code here..
}
catch(Exception e) { //----------2
e.printStackTrace();
}

Yogen Vadnere
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Catching a SQLException