finally block will not be executed if any exception raised in finally. Because exception behaves the same for all blocks.
public class Exception in ExceptionRaisedInFinallyBlock
{
public static void main(String args[])
{
try
{
System.out.println("This code is inside in the try block");
System.out.println("Rest code inside the try block");
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("If any exception raised in try block then catch block
will execute");
}
finally
{
int a = 2/0;
System.out.println("This is finally block");
}
//rest code of the program
System.out.println("This code is outside from the try block");
}
}
Output: This code is inside in the try block Rest code inside the try block Exception in thread “main” java.lang.ArithmeticException: / by zero at ExceptionRaisedInFinallyBlock.main(ExceptionRaisedInFinallyBlock.java:18)