Java finally keyword is used to define a finally block in a Java program. The finally block in Java follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception in a Java program.
Using a Java finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
A finally block appears at the end of the catch blocks in Java program and has the following syntax −
Syntaxtry { // Protected code } catch (ExceptionType1 e1) { // Catch block } catch (ExceptionType2 e2) { // Catch block } catch (ExceptionType3 e3) { // Catch block }finally { // The finally block always executes. }Example
In this example, we're accessing an element of an array using invalid index. When program runs, it throws ArrayIndexOutOfBoundsException. As we're handling this exception in catch block, the following finally block is also executed.
// File Name : ExcepTest.java package com.tutorialspoint; public class ExcepTest { public static void main(String args[]) { int a[] = new int[2]; try { System.out.println("Access element three :" + a[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Exception thrown :" + e); }finally { a[0] = 6; System.out.println("First element value: " + a[0]); System.out.println("The finally statement is executed"); } } }Output
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executedExample
In this example, we're accessing an element of an array using invalid index. When program runs, it throws ArrayIndexOutOfBoundsException. As we're handling this exception in catch block, and throwing an exception further then following finally block will still be executed.
// File Name : ExcepTest.java package com.tutorialspoint; public class ExcepTest { public static void main(String args[]) throws CustomException { int a[] = new int[2]; try { System.out.println("Access element three :" + a[3]); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Exception thrown :" + e); throw new CustomException(e); }finally { a[0] = 6; System.out.println("First element value: " + a[0]); System.out.println("The finally statement is executed"); } } } class CustomException extends Exception{ private static final long serialVersionUID = 1L; CustomException(Exception e){ super(e); } }Output
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 Exception in thread "main" First element value: 6 The finally statement is executed com.tutorialspoint.CustomException: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.main(ExcepTest.java:12) Caused by: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.main(ExcepTest.java:9)
Note the following −
A catch clause cannot exist without a try statement.
It is not compulsory to have finally clauses whenever a try/catch block is present.
The try block cannot be present without either catch clause or finally clause.
Any code cannot be present in between the try, catch, finally blocks.
java_basic_syntax.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4