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();
            }
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

colr__
0
william007Author Commented:
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?



0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

kjetijoCommented:
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.
0
william007Author Commented:
>>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.
0
colr__Commented:
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__
0
kjetijoCommented:
There are exceptions that the eclipse-compiler don't require that you put in a try-catch block. Like NullPointerException and IndexOutOfBoundsException. but theese exceptions are by default thrown when they arrive anyway, so with the specific code you've written it'll just do what java would have done by default anyway.

So if you're planing on handling the exception, you can leave the catch(Exception e) there, but if you're just going to throw it on anyway, you can cut it out...
0
colr__Commented:
Only if you're code causes it to, for example trying to call a method on a null object will throw a null pointer exception.

colr__
0
objectsCommented:
> 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, it could throw a RuntimeException (or subclass). methods do not need to declare that they throw them.
0
Sys_ProgCommented:
Please read thru the Java Exception Handling mechanism.
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

Java does not need you to handle Runtime Exceptions. Apart from those, all other exceptions are either to be caught OR thrown from the method and in the latter case, your method signature should say that.
Thus, in your case, Eclipse editor would compell you to catch only those Exveptions which are declared to be thrown from the methods you are calling within your code.

Regarding Runtime Excpetions, they can occur at almost every line of code. Hence I guess Java has classified them as Runtime and doesnt require us to declare them in each method. The following link gives a detailed explanation of the same
http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

HTH
Amit
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
william007Author Commented:
Thanks:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.