Re: Purpose of FINALLY Block
[QUOTE=Geek_Guest;21541]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?
[b]Question asked by visitor Sudheer[/b][/QUOTE]
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.
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
Re: Purpose of FINALLY Block
[B]finally block[/B]
- 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
Re: Purpose of FINALLY Block
[QUOTE=rahulvegi;23016][B]finally block[/B]
- 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[/QUOTE]
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!
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
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.
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.
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.
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
}