使用 finally 块捕获异常的Java程序
Java中的finally块用于放置重要的代码,例如清理代码,例如关闭文件或关闭连接。 finally 块会执行异常是否出现以及异常是否被处理。无论异常是否发生,finally 都包含所有关键语句。
有 3 种可能的情况可以使用 finally 块:
案例1:当异常没有上升时
在这种情况下,程序运行良好,不会抛出任何异常,并在 try 块之后 finally 块执行。
Java
// Java program to demonstrate
// finally block in java When
// exception does not rise
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("inside try block");
// Not throw any exception
System.out.println(34 / 2);
}
// Not execute in this case
catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
}
// Always execute
finally {
System.out.println(
"finally : i execute always.");
}
}
}
Java
// Java program to demonstrate finally block in java
// When exception rise and handled by catch
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// catch an Arithmetic exception
catch (ArithmeticException e) {
System.out.println(
"catch : exception handled.");
}
// Always execute
finally {
System.out.println("finally : i execute always.");
}
}
}
Java
// Java program to demonstrate finally block
// When exception rise and not handled by catch
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("Inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// Can not accept Arithmetic type exception
// Only accept Null Pointer type Exception
catch (NullPointerException e) {
System.out.println(
"catch : exception not handled.");
}
// Always execute
finally {
System.out.println(
"finally : i will execute always.");
}
// This will not execute
System.out.println("i want to run");
}
}
输出
inside try block
17
finally : i execute always.
情况二:当异常上升并被catch块处理时
在这种情况下,程序抛出异常但由 catch 块处理,并且 finally 块在 catch 块之后执行。
Java
// Java program to demonstrate finally block in java
// When exception rise and handled by catch
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// catch an Arithmetic exception
catch (ArithmeticException e) {
System.out.println(
"catch : exception handled.");
}
// Always execute
finally {
System.out.println("finally : i execute always.");
}
}
}
输出
inside try block
catch : exception handled.
finally : i execute always.
情况 3:当异常上升且未被 catch 块处理时
在这种情况下,程序抛出异常但没有被catch处理,所以finally块在try块之后执行,finally块执行后程序异常终止,但finally块执行正常。
Java
// Java program to demonstrate finally block
// When exception rise and not handled by catch
import java.io.*;
class GFG {
public static void main(String[] args)
{
try {
System.out.println("Inside try block");
// Throw an Arithmetic exception
System.out.println(34 / 0);
}
// Can not accept Arithmetic type exception
// Only accept Null Pointer type Exception
catch (NullPointerException e) {
System.out.println(
"catch : exception not handled.");
}
// Always execute
finally {
System.out.println(
"finally : i will execute always.");
}
// This will not execute
System.out.println("i want to run");
}
}
输出
Inside try block
finally : i will execute always.
Exception in thread "main" java.lang.ArithmeticException: / by zero
at GFG.main(File.java:10)