Finally Block In Java Example
Chapter:
Exception Handling
Last Updated:
08-06-2017 14:12:43 UTC
Program:
/* ............... START ............... */
public class JavaFinallyExample {
public static void main(String args[]) {
try {
int value = 100 / 5;
System.out.println(value);
} catch (NullPointerException e) {
System.out.println(e);
} finally {
System.out.println("finally block will always execute");
}
}
}
/* ............... END ............... */
Output
20
finally block will always execute
Notes:
-
Java finally block is always executed whether exception is handled or not.
- The finally block always executes when the try block exits.
- Finally block will execute even if an unexpected exception occurs or not.
- Java finally block is a block that is used to execute important code such as closing connection, stream etc.
- Finally block in java can be used to put "cleanup" code such as closing a file, closing connection etc.
Tags
Finally Block, Java