The
finally block always executes when the try block exits. This ensures
that the finally block is executed even if an unexpected exception
occurs. Finally block mainly used to perform clean up operation like
closing file streams, flushing the data etc.,
A
try block must associate with at least one catch or finally block.
Example
class ExceptionEx{ public static void main(String args[]){ try{ System.out.println(10/0); } finally{ System.out.println("I am finally block"); } System.out.println("I am not going to executed"); } }
Output
I am finally block Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionEx.main(ExceptionEx.java:5)
Observe
the output, before throwing the exception, the code in the finally is
executed. One more thing is above program doesn't handle the
exception, so after executing the finally block, Java run time throws
the exception and terminates the program.
finally
always executes even the exception handled by the catch block.
Example
class ExceptionEx{ public static void main(String args[]){ try{ System.out.println(10/0); } catch(Exception e){ System.out.println(e); } finally{ System.out.println("I am finally block"); } System.out.println("I am done with execution"); } }
Output
java.lang.ArithmeticException: / by zero I am finally block I am done with execution
The runtime system always executes the statements within the finally block regardless of what happens within the try block.
class ExceptionEx{ static int print(){ try{ return 10; } finally{ System.out.println("I am finally block"); } } public static void main(String args[]){ int var = print(); System.out.println("value of var is" + var); } }
Output
I am finally block
value of var is10
Above
program called the return statement, even then also finally executed.
Some
Points To Remember
1.
What is the Output of the below Program ?
class ExceptionEx{ static int print(){ try{ return 10; } finally{ return -1; } } public static void main(String args[]){ int var = print(); System.out.println("value of var is " + var); } }
Output
value of var is -1
Since
the finally block execute even prgram calls return from the try
block. So the value returned by try block is 100 overridden by the
value -1.
2.
What is the output of the below Program ?
class ExceptionEx{ static int print(){ try{ System.out.println(10/0); } catch(ArithmeticException e){ System.out.println(e); return 100; } finally{ return -1; } } public static void main(String args[]){ int var = print(); System.out.println("value of var is " + var); } }
Output
java.lang.ArithmeticException: / by zero value of var is -1
3.
What are the situation that finally can't execute ?
a.
If the JVM exits while the try or catch code is being executed, then
the finally block may not execute
b.
if the thread executing the try or catch code is interrupted or
killed, the finally block may not execute.
4.
What is the output of the below program ?
class ExceptionEx{ static void print(){ try{ System.out.println("I am in try"); System.exit(0); } finally{ System.out.println("I am in finally"); } } public static void main(String args[]){ print(); } }
Output
I am in try
Calling
system.exit() terminates the program, so finally can't execute.
No comments:
Post a Comment