Geeks Talk

Prepare for your Next Interview




Purpose of FINALLY Block

This is a discussion on Purpose of FINALLY Block within the Java forums, part of the Software Development category; What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory ...


Go Back   Geeks Talk > Software Development > Java

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 11-27-2007
Expert Member
 
Join Date: Feb 2007
Posts: 1,279
Thanks: 0
Thanked 161 Times in 137 Posts
Geek_Guest is on a distinguished roadGeek_Guest is on a distinguished road
Purpose of FINALLY Block

What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory statements are written out side the catch block. so pls tell me finally block need?

Question asked by visitor Sudheer
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-27-2007
Contributing Member
 
Join Date: Oct 2007
Location: trichy
Posts: 92
Thanks: 2
Thanked 10 Times in 8 Posts
sarathi trichy is on a distinguished road
Smile Re: Purpose of FINALLY Block

Quote:
Originally Posted by Geek_Guest View Post
What is the purpose of finally block (i know Mandatory statements are written in finally block) but outside of the catch block statements are also executed right i write mandatory statements are written out side the catch block. so pls tell me finally block need?

Question asked by visitor Sudheer

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
Reply With Quote
  #3 (permalink)  
Old 12-04-2007
Junior Member
 
Join Date: Dec 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
praveen.m is on a distinguished road
Re: Purpose of FINALLY Block

The Main purpose of Finally Block is to close all the files that are opened in the code or any datebases in the code.Here(in Finally Block)we write code to close all datebases ,files etc.
The Finally Block will Execute Wheteher there is an Exception or not
Reply With Quote
  #4 (permalink)  
Old 12-21-2007
Contributing Member
 
Join Date: Nov 2007
Location: bangalore
Posts: 54
Thanks: 6
Thanked 3 Times in 3 Posts
rahulvegi is on a distinguished road
Re: Purpose of FINALLY Block

finally block
- it handles uncatched exceptions.
- this must be followed with the try and catch block.
- if try block throw an exception then no other catch block handle that exception. now finally block executes that exception.
example:

try {
//it throws a zero by division error

}
catch(NullPointerException)
{
// it throws NullPointerException instead of Zero by division error
}
finally{
printStackTrace();
}
- in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption.
main advantage is to be finally block must be executed.

note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error
Reply With Quote
The Following User Says Thank You to rahulvegi For This Useful Post:
  #5 (permalink)  
Old 07-06-2008
Junior Member
 
Join Date: May 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
tal.benavraham is on a distinguished road
Smile Re: Purpose of FINALLY Block

Quote:
Originally Posted by rahulvegi View Post
finally block
- it handles uncatched exceptions.
- this must be followed with the try and catch block.
- if try block throw an exception then no other catch block handle that exception. now finally block executes that exception.
example:

try {
//it throws a zero by division error

}
catch(NullPointerException)
{
// it throws NullPointerException instead of Zero by division error
}
finally{
printStackTrace();
}
- in the above example no catch statement to handle the exception i.e throw by the try then finally block can handle that exception by using the printstacktrace() method. printstacktrace(), which handles any other exption.
main advantage is to be finally block must be executed.

note:dont enter any statement between try,catch and finally block even System.out.print() also. if u use compiler gives an error

1) please note that the finally block will ALLWAYS be executed, no matter what. not just if an exception was not caught.
2) what happends if an exception is thrown from within the finally block? - aha: the execution of the finally code will stop and the exception will be thrown out of the current function!
Reply With Quote
  #6 (permalink)  
Old 07-07-2008
Junior Member
 
Join Date: Jul 2008
Location: india
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
rsharma052011 is on a distinguished road
Re: Purpose of FINALLY Block

the Finally is to ensure tht the code enclosed with in this block is xecuted ever if when our program teriminates due uncatched xception
Reply With Quote
  #7 (permalink)  
Old 07-07-2008
Junior Member
 
Join Date: Jul 2008
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
shashipal is on a distinguished road
Re: Purpose of FINALLY Block

finaly block is executed whether or not an exception accures.it will be executed always .The
main purpose of this is to execute the code that we have to execute in every condition.
for example we have to close a database connection whether our command executes succesfully or not
Try
{
myComm.executeNonQuery();
}
catch(Ex)
{
//do something with exception Ex
return;
}
finaly
{
myConn.close();
}
the main point to see in above code that whether we have applied a return statement in catch block finaly block will still execute.
Reply With Quote
  #8 (permalink)  
Old 07-07-2008
Junior Member
 
Join Date: Jul 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Sukumar Maddineni is on a distinguished road
Smile Re: Purpose of FINALLY Block

Purpose of finally block :

--> The finally block is self explanatory(finallyyyyyy).

--> The piece of code which you want to execute after come out of
statements in try block must and should must be written in finally block.
i.e independent of reslut of try bolck code whether it is successful or not .

Ex :

public class FinallyTest
{
FinallyTest()
{
String str = "1";
try
{
//try to get array or stringIndexOutOfBoundsException

System.out.println(str.charAt(2));
}
catch(NumberFormatException ne)
{
ne.printStackTrace();
System.out.println("catch called");
}
finally
{
System.out.println("finally called");
}

System.out.println("method end called");
}

public static void main(String args[])
{
FinallyTest objTest1 = new FinallyTest();
}
}
--> Your questiona about after catch block statments also will be executed
the why finally ,

Consider above example,
In that method StringIndexOutOdBOundsException
occured which is not caught.

so statements below catch cannot be executed.
But statments in finally are executed.

The statments below catch block are executed only
if you caught the raised exception.

But the statements in finally block are executed
even though you caught or uncaught the raised exception


--> So now I think You Got IT.
Reply With Quote
  #9 (permalink)  
Old 07-28-2008
Junior Member
 
Join Date: Jul 2008
Location: Bangalore
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
debabrata.mallick7 is on a distinguished road
Thumbs up Re: Purpose of FINALLY Block

Actually out side the catch block u can write the mandatory codes but when an exception occurred before the mandatory statement then these codes are not
executed so in a program finally block are execute, whether there is some exception occur or not, so the mandatory statement must execute.
Reply With Quote
  #10 (permalink)  
Old 07-29-2008
Junior Member
 
Join Date: Jul 2008
Location: Bangalore
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
anand.badiger is on a distinguished road
Smile Re: Purpose of FINALLY Block

Hi,
Follow the snippest code here
try
{
// do something here
}
catch
{
// If any errors are there then the control will return from here without //executing next code after this catch block
}
to do some operation if any exception is thrown from the try block you need finally
{
// do other oprations
}
Reply With Quote
Reply

  Geeks Talk > Software Development > Java


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
Anonymous Block krishnaindia2007 Oracle 4 07-26-2008 12:32 PM
What is the purpose of 'if not itab is initial' Geek_Guest SAP R/3 1 02-01-2008 03:41 AM
what is the purpose of using UNIX in software testing ? please explain in details fkrgullu Unix/Linux 1 06-16-2007 04:48 PM
Purpose of testing sakshi_2801 Testing Issues 3 02-27-2007 08:04 AM
Is this command serve the purpose of renaming christia Unix/Linux 1 09-09-2006 04:18 PM


All times are GMT -4. The time now is 05:37 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved