Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

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 Blogs FAQ Tag Cloud Calendar Mark Forums Read

Java Java related Issues and Problems

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-27-2007
Expert Member
 
Join Date: Feb 2007
Posts: 1,279
Thanks: 0
Thanked 192 Times in 154 Posts
Geek_Guest has a spectacular aura aboutGeek_Guest has a spectacular aura aboutGeek_Guest has a spectacular aura about
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 13 Times in 9 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 6 Times in 4 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-07-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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

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


All times are GMT -4. The time now is 09:59 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved