Java 简明教程
Java - Finally Block
The finally Block in Java
最终于块紧跟 a try block or a catch block 后。无论是否发生异常,最终于块的代码都会始终执行。
The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.
使用 finally 块允许你运行你想要执行的任何清理类型语句,无论受保护代码中发生什么。
Using a finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.
Syntax: Finally Block
finally 块出现在 catch 块的末尾并且具有以下语法 −
A finally block appears at the end of the catch blocks and has the following syntax −
try {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}
Points To Remember While Using Finally Block
-
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.
-
finally block is not executed in case exit() method is called before finally block or a fatal error occurs in program execution.
-
finally block is executed even method returns a value before finally block.
Why Java Finally Block Used?
-
Java finally block can be used for clean-up (closing) the connections, files opened, streams, etc. those must be closed before exiting the program.
-
It can also be used to print some final information.
Java Finally Block Example
以下代码段展示了如何在处理异常后在 try/catch 语句后使用 finally。在此示例中,我们创建了一个错误,即使用无效的索引访问数组元素。catch 块正在处理异常并输出它。现在在 finally 块中,我们正在输出一条语句,表示 finally 块正在执行。
Here is code segment showing how to use finally after try/catch statements after handling exception. In this example, we’re creating an error accessing an element of an array using invalid index. The catch block is handling the exception and printing the same. Now in finally block, we’re printing a statement signifying that finally block is getting executed.
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");
}
}
}
Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3
First element value: 6
The finally statement is executed
More Examples
Example 1
以下代码段展示了即使未处理异常,如何使用 finally 块在 try/catch 语句之后。在此示例中,我们创建了一个错误,即使用无效的索引访问数组元素。由于 catch 块没有处理异常,我们可以在输出中检查 finally 块正在输出一条语句来表示 finally 块正在执行。
Here is code segment showing how to use finally after try/catch statements even exception is not handled. In this example, we’re creating an error accessing an element of an array using invalid index. As the catch block is not handling the exception, we can check in output that finally block is printing a statement signifying that finally block is getting executed.
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 (ArithmeticException 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");
}
}
}
First element value: 6
The finally statement is executed
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8)
Example 2
以下代码段展示了如何使用 finally 块,其中一个方法可以在 try 块中返回一个值。在此示例中,我们在 try 块中返回一个值。我们可以在输出中检查,即使方法向调用函数返回一个值,finally 块也会输出一条语句,表示 finally 块正在执行。
Here is code segment showing how to use finally block where a method can return a value within try block. In this example, we’re returning a value within try block. We can check in output that finally block is printing a statement signifying that finally block is getting executed even after method returned a value to caller function.
package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
System.out.println(testFinallyBlock());
}
private static int testFinallyBlock() {
int a[] = new int[2];
try {
return 1;
} 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");
}
return 0;
}
}
First element value: 6
The finally statement is executed
1