william007
asked on
About Exception
I am using Eclipse as editor
try {
conn = getConn();
String sSQL = "INSERT INTO myTable";
sSQL += " values (?)";
prepStat = conn.prepareStatement(sSQL );
prepStat.setTimestamp(1, alm.getTimeStamp());
return prepStat.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
closeDBResources();
}
After catching the SQLException, IDE does not complaint for the unhandledexception.
But is there a need to catch a generic exception to prevent any exception other then SQLException? Is it is, what is the example?
try {
conn = getConn();
String sSQL = "INSERT INTO myTable";
sSQL += " values (?)";
prepStat = conn.prepareStatement(sSQL );
prepStat.setTimestamp(1, alm.getTimeStamp());
return prepStat.executeUpdate();
} catch (SQLException e) { //<--this
throw e;
} catch (Exception e){
throw e;
}
} finally {
closeDBResources();
}
try {
conn = getConn();
String sSQL = "INSERT INTO myTable";
sSQL += " values (?)";
prepStat = conn.prepareStatement(sSQL
prepStat.setTimestamp(1, alm.getTimeStamp());
return prepStat.executeUpdate();
} catch (SQLException e) {
throw e;
} finally {
closeDBResources();
}
After catching the SQLException, IDE does not complaint for the unhandledexception.
But is there a need to catch a generic exception to prevent any exception other then SQLException? Is it is, what is the example?
try {
conn = getConn();
String sSQL = "INSERT INTO myTable";
sSQL += " values (?)";
prepStat = conn.prepareStatement(sSQL
prepStat.setTimestamp(1, alm.getTimeStamp());
return prepStat.executeUpdate();
} catch (SQLException e) { //<--this
throw e;
} catch (Exception e){
throw e;
}
} finally {
closeDBResources();
}
The IDE (or the compiler for that matter) won't complain as what you have done is legal code, although a bit useless.
colr__
colr__
ASKER
Hi,
What I mean is I worry about if I do not catch(Exception e) at the last line, there may be some exception(other then SQLException) being throw in some error condition, although Java API does not stipulate I need to catch other exception.
Is my worry redundant?
What I mean is I worry about if I do not catch(Exception e) at the last line, there may be some exception(other then SQLException) being throw in some error condition, although Java API does not stipulate I need to catch other exception.
Is my worry redundant?
As allready pointed out, you're not really doing anything with this try-catch rutine. You're just catching the exception and throwing it off again. This is quite allright if you're in a method and want to take care of the exception in another method that uses this one. In that case you'll have to add "throws SQLException" after the method decleration. If on the other hand, you're in the main-method, this won't do you any good, since there's noone else to catch your exception and the code is once again pointless. However I do think that it is required to type throws SQLException even after the main-method in order to get it all to compile.
ASKER
>>This is quite allright if you're in a method and want to take care of the exception in another method that uses this one
yes, in the method, I put "throws SQLException"
But now I am not worry about SQLException, since I have throws out for the calling method to handle it, what I worry about is the Exception other than SQLException.
What I mean is, will there be a case that the program throws an Exception other than SQLException, although java API does not need me to handle it.
yes, in the method, I put "throws SQLException"
But now I am not worry about SQLException, since I have throws out for the calling method to handle it, what I worry about is the Exception other than SQLException.
What I mean is, will there be a case that the program throws an Exception other than SQLException, although java API does not need me to handle it.
I wouldnt worry about it. The only real threat here is runtime exceptions, which you cant really control anyway.
Just ignore it. If on the other hand you are writing life-critical s/w, then youll want to check for and handle any possible error conditions, but for most cases, you can just leave this out.
colr__
Just ignore it. If on the other hand you are writing life-critical s/w, then youll want to check for and handle any possible error conditions, but for most cases, you can just leave this out.
colr__
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks:-)
The samne applies to your generic Exception line - your just catching it and throwing it again, which there doesnt seem much point in,.
What exactly are you trying to do here? If you are trying to ignore the exceptions that are raised, catch them and do nothing, like the folloinwg:
catch (SQLException e){;}
catch (Exception e){;}
colr__