• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in try,catch,finally  RSS feed

 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

see the below code...



when i run this code it's give me a error like this..


TestDatabase.java:22: error: unreported exception SQLException; must be caught or declared to be thr
own
con.close();


i can't understand why this compile error occur
i think i handle this SQL Exception correctly. am i correct?

but i run above code like this way it's not give me any error...



please can any one tell me what is the reason for that...
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried putting it in the try block?
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think when you declare `throws` in combination with `try-catch-finally` the compiler assumes that the caller of your method is required the catch the exception. Thats why I feel it compiles the code as it is in the second case.

As to your first case, closing the connection in the `try` seems to not make the compiler complain. However, I might be just doing the guessing without anything to back up with.

As an aside, you should not have an empty catch block because it swallows exceptions. Atleast have a `printStackTrace()` in there.



 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joel Christophel
what you mean "Have you tried putting it in the try block?"
i think i already done it that in my problem


Ahsan Bagwan


yes may be you are correct i also think that way, but i don't know why try, catch block not working for that problem. i mean why try,catch block can't handle that checked Exception
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also thanks for all your valuable comments
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you fix this so that it compiles, you will get an exception when you try to run it. The cause of the exception you get is the following:

The class is named sun.jdbc.odbc.JdbcOdbcDriver, not sun.jdbc.odbc.jdbcodbcDriver.

Note the difference in case: JdbcOdbcDriver is not the same as jdbcodbcDriver. You need to change the class name in your code.

Also: Never catch an exception and then ignore it, by having an empty catch-block. Print the stack trace, otherwise you will not see anything when an exception happens:
 
Balaji Manoharan Bm
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think it is answered by Ahsan above. Am putting it differently to make it simple. The problem is due to con.close() which throws SQLException and it is a checked exception and so it must be caught or thrown.

surround your con.close() like this
try {
con.close();
} catch (SQLException se){
se.printStackTrace();
}

If you copy the code and put it in a different method, you can simply understand the issue.

for e.g

public static void main(Strings[] args) {

try {
Connection con = getConnection();

} catch (Exception ex){

}

}

private static Connection getConnection() throws SQLException {

}
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randika Isuru wrote:
what you mean "Have you tried putting it in the try block?"
i think i already done it that in my problem


No. In your problem, you are closing the connection in the finally block.
 
Ritobroto Seth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Randika,

In your first code block just put the
con.close();
statement inside the try block. This will prevent the compilation error.

con.close() is a checked exception and it should be handled in your code.

Your second code block doesnot give any compilation error because you are declaring an exception in the main method --
public static void main (String[] args)throws Exception

so the statemnet con.close() is taken care of in the second code block.
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, now i get well understand about my problem because your all...
thank you very much all of you for your valuable comments and also spend your valuable time to solve this problem for me..
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritobroto Seth wrote:Hi Randika,

In your first code block just put the
con.close();
statement inside the try block. This will prevent the compilation error. . . .
But it can introduce a runtime error if the Exception occurs earlier. You will have the connection always open, and there may be no access to your database. That is why the close() call needs to be inside a finally block.
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie
That is why the close() call needs to be inside a finally block.


yes, you are correct but how can do it using try, catch block. so i think i can't do it using try catch block. The only way to do it declare throws Exception in main method am i correct?
because with declare a throws Exception when i put close() method inside a finally block using try catch block it give me a compile time error. because of con.close() method pass the checked exception so i must need to handle or declare that.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randika Isuru wrote: . . . . The only way to do it declare throws Exception in main method am i correct?. . .
No. Have look in the Java tutorials, where you will find an example with a finally. I think it is in the “processing SQL statements…” section.

OR…

Use Java7 and a try‑with‑resources block.
 
Randika Isuru
Ranch Hand
Posts: 38
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie :


Ok thank you very much for share your knowledge with me.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!