DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

Query on Finally block:

By: Admin | 09 Mar 2010 12:05 pm

Hi,

What is the reason of having finally block after try catch block ? I know this block of code will execute irrespective of whether exception occurs or not. 
Without finally block also, the code after try catch block will execute right?
Please clarify me this doubt.

Thanks,

Comments

Hi,

As you stated in your question, the code inside the finally block with normally execute irrespective of what happens, whether an exception occurs or not. This area of code is therefore used for necessary activities such as closing of opened database connections and releasing of acquired resources. Without a finally block, an exception will bubble out of the application and go to the Catch block so that everything after the code that gave the exception will not be implemented. To ensure that a piece of code is always implemented whether or not an exception occur, you need the finally block. I hope this helps. Best regards,

By: Admin | 09 Mar 2010
Hi, Many thanks for the reply. I can close the database connection and release resources outside finally block which will anyway execute after execution of try catch block. Then why should I have finally block? Thanks,
By: Admin | 09 Mar 2010

may be this example will help you
 
case 1 "code 1" do not throw any exception
try{
code 1
code 2
}
catch(Exception e){
handle exception
}
finally{
finally code
}
extra code
 
 
in this case "handle exception will not be executed", "finally code" will be executed "extra code" will be executed
 
case 2 "code 1" throw an exception
try{
code 1
code 2
}
catch(Exception e){
handle exception
}
finally{
finally code
}
extra code
 
"code 2" will not be executed "handle exception" will be executed, "finally code" will be executed, "extra code" will be executed
 
case 3 "code 1" throw and exception
try{
code 1
code 2
}
finally{
finally code
}
extra code
 
"code 2" will not be executed, "finally code" will be executed, "extra code" will not be executed and the method will exit throwing the exception
 
case 4 "code 1" throw and exception
try{
code 1
code 2
}
catch(Exception e){
handle exception
throw
}
finally{
finally code
}
extra code
 
"code 2" will not be executed "handle exception" will be executed, "finally code" will be executed, "extra code" will not be executed"
 
The point here is how to best handle the exception for example you make a query call it throw an exception you can have just a finally block to close the connection if is open and let the exception bubble up. like in case 3. Or you can wrap the exception in your own exception and throw it up. The goal in this example is close the connection if not you will have a database connection leak that will kill your app. Second do not catch the exception if you are not going to handle it. If you just catch the exception and ignore it you can end with a nasty bug hard to debug. For example you can end up with a null exception because in the "extra code" you are using a variable that is never set to a value because your query throw an exception you caught it and ignore it and this code got executed.
 
why use finally to avoid duplicate code
 
with out finally you will need this code
 
try{
query
}
catch(Exception e)
{
close connection
}
close connection
 
with finally
 
try{
query
}
finally
{
close connection
}
 
 

By: Admin | 09 Mar 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move