[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

About Exception

Posted on 2006-04-20
11
Medium Priority
?
253 Views
Last Modified: 2008-02-01
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();
            }
0
Comment
Question by:william007
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 8

Expert Comment

by:colr__
ID: 16495874
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
 
LVL 8

Expert Comment

by:colr__
ID: 16495880
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
 
LVL 9

Author Comment

by:william007
ID: 16495948
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:kjetijo
ID: 16495949
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
 
LVL 9

Author Comment

by:william007
ID: 16495977
>>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
 
LVL 8

Expert Comment

by:colr__
ID: 16495981
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
 
LVL 1

Assisted Solution

by:kjetijo
kjetijo earned 200 total points
ID: 16495985
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
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 200 total points
ID: 16495987
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
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 16496098
> 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
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 1200 total points
ID: 16496366
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
 
LVL 9

Author Comment

by:william007
ID: 16517569
Thanks:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question