![]() |
![]() |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Introduction of Errors | |
Java Exceptions | |
Java Try Block | |
Java Catch Block | |
Java Finally Block | |
Java Throw Keyword | |
Java Throws Keyword | |
Java Builtin Exceptions | |
User Defined Exceptions |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
If the exception is not caught by any catch block then finally block is responsible for handling the exception.
finally block statements are always executed after the try catch block has completed its execution.
Rules for writting finally block
try { // statements that generates an exception .... .... } catch (Exception-type1 object1) { // Code for solving the Exception-type1 .... .... } . . finally { // finally block code compulsory executed after try and catch ... ... }
Note : finally do not contain any argument list like catch block.
Example
try { int a=25, b=0; int c = a/b; // generates an exception as Divided by zero. System.out.println("Division is : "+c); // executed if there is no Exception. } catch(ArithmeticException AE) { System.out.println("Divided by zero error. Please change inputs"); // take action if error occurred. But never goes to try block. } finally { System.out.println("Finally block executed"); }