• Dear Guest, Please do NOT forget to join our JAVA JOBS (ONLY) FACEBOOK GROUP to be aware of latest JAVA openings, and you may also post any JAVA related queries/jobs.

What happens if an exception occurs in finally block??

#3
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.
:)
 
#4
I have been recently asked in an interview

what happens if the code in finally block causes and exception??
Hi, Venkat,

See Finally is also a block, where code gets executed. and it is possible to occurs Exception there. and if any Exception occurs in finally result is Same Termination of the Program.
Solution : Use try Catch blocks there to avoid termination.
 
#6
hi venkat,
iam naveena. u asked one question that what happens if an exception occurs in finally block. can u tell me the exact answer.....
 
#7
Code:
try{

   //call to method which throws exception called SomeException

}catch(SomeException exception){
    //Handle SomeException here

} finally{
     try{
         //call to method which throws exception called OtherException
    }catch(OtherExceptionexception){
         //Handle SomeException here

    }// end of inner try-catch block


}// end of Outer try-catch-finally block
As simple as that :)
 
#8
ya there is a possibility that exception may occur in finally block also so the program will stop its excution abnormally if the outer catch block is not there but it will run normally if the outer catch catches the exception which is raised in finally block.
 
#9
hi ,
The following is the simple example :

public class ExceptionTest {

public static void main(String args[])
{
try
{
System.out.println("Inside Try");
}catch(Exception e)
{
System.out.println("Inside catch");
}
finally
{
try
{
int i=10/0;
}catch(Exception e)
{
System.out.println(e);
}
}
}

}

exception may arise in finally block . To avoid automatic termination we can use try catch inside finally .....
 
Top