Link to home
Start Free TrialLog in
Avatar of william007
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();
            }
Avatar of colr__
colr__

With your 'catch (SQLException e) throw e' statement, you arenet in fact doing anything - you are catching a thrown exception, then throwing it again straight away. Note that in this case, you are catching and throwning an SQLException, so you arent even changing the Exception type.

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__
The IDE (or the compiler for that matter) won't complain as what you have done is legal code, although a bit useless.

colr__
Avatar of william007

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?



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.
>>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.
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__
SOLUTION
Avatar of kjetijo
kjetijo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks:-)