hi, would you please tell me the answer for the query?
In try block i've written code to establish database connection and in finally block i'd closed the connection. If any exception is not raised then no problem. If an exception is raised while creating database connection object then connection object is not created then to statements in finally block will be executed as default.
How it is possible that with out creating an obj how can we close it?
Thank U for showing interest to reply. i know the code to close connection. what my query is if an exception is raised while establishing connection, connection object is not created.But by default the statements in finally block are executed where we've written code to close the connection. But connection object is not created.
Yuk. Bad style. Gabby's is superior and also quite common.
I recall another thread that a similar situation with files was discussed. In fact, I think I started this other thread, but I cannot find it. Someone posted in that thread something similar to Julien's code here and rattled off some reasons why it is preferred. If you know the thread I'm talking about, a link would be nice
I remember that too. It was Tony Morris, I think, who was saying that the nested try blocks were the way to go. Personally I don't like it -- but I can understand the logic. One reason I'm wary of nested try blocks is that I've seen decompilers and byte-code instrumentation tools trip over them. The generated code can be messy indeed.
But what I was complaining about is actually unrelated -- not the nested try blocks, but allowing the NullPointerException to be thrown and then caught when you try to close the nonexistent Connection. Not only does this have bad performance (letting that easily-avoided exception be thrown when you know throwing exceptions is costly), but it also masks what the original exception was if there was an exception opening the connection!
Originally posted by Ernest Friedman-Hill: But what I was complaining about is actually unrelated -- not the nested try blocks, but allowing the NullPointerException to be thrown and then caught when you try to close the nonexistent Connection.
Take a look at the code again. The connection *cannot* be null in the finally block.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
subject: how to close connection object in finally block when an exception is raised in try bl