Finally block:
It is never recommended to take clean up code inside try block because there is no guarantee for the execution of every statement inside a try.
It is never recommended to place clean up code inside the catch block because if there is no exception then catch block won’t be executed.
We require someplace to maintain clean up code which should be executed always irrespective of whether exception raised or not raised and whether handled or not handled such type of place is nothing but finally block.
Hence the main objective of finally block is to maintain cleanup code.
Example:
try
{
risky code
}
catch(x e)
{
handling code
}
finally
{
cleanup code
}
• The specialty of finally block is it will be executed always irrespective of whether the exception raised or not raised and whether handled or not handled.
Example 1:
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
}
}
Output:
Try block executed
Finally block executed
Example 2:
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
System.out.println(10/0);
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
}
}
Output:
Try block executed
Catch block executed
Finally block executed
Example 3:
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
System.out.println(10/0);
}
catch(NullPointerException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
}
}
Output:
Try block executed
Finally block executed
Exception in thread "main" java.lang.ArithmeticException: / by zero at Test.main(Test.java:8)
Return Vs Finally:
Even though return present in try or catch blocks first finally will be executed and after that only return statement will be considered that is finally block dominates return statement.
Example:
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try block executed");
return;
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
}
}
Output:
Try block executed
Finally block executed
If return statement present try catch and finally blocks then finally block return statement will be considered.
Example:
class Test
{
public static void main(String[] args)
{
System.out.println(methodOne());
}
public static int methodOne()
{
try
{
System.out.println(10/0);
return 777;
}
catch(ArithmeticException e)
{
return 888;
}
finally
{
return 999;
}
}
}
Output:
999
• There is only one situation where the finally block won’t be executed is whenever we are using System.exit(0) method.
Example:
class Test
{
public static void main(String[] args)
{
try
{
System.out.println("try");
System.exit(0);
}
catch(ArithmeticException e)
{
System.out.println("catch block executed");
}
finally
{
System.out.println("finally block executed");
}
}
}
Output:
Try
Comments
Post a Comment