Java 异常处理
Java 异常处理很重要,如果不处理异常可能会导致程序的终止。在本教程中,我们将借助示例了解 Java 中不同的异常处理方法。
在本教程中,我们将借助示例了解 Java 中不同的异常处理方法。
在上一个教程中,我们了解了 Java 异常。如果不处理异常可能会导致程序的终止
Java 中处理异常主要涉及到以下内容:
- 使用
try...catch
语句捕获异常并处理 - 使用
throws
关键字为方法声明需要处理的异常 - 使用
throw
关键字抛出一个异常
try…catch
在 Java 中,使用 try-catch
块捕获和处理异常。下面是 try...catch
块的语法:
try {
// code
}
catch(Exception e) {
// code
}
在这里,我们将可能产生异常的代码放置在 try
块内,catch
块用来捕获对应的异常并进行处理。
try...catch
块中可以包含 0 个或多个 catch
,以应对多个不同的异常。catch
必须跟随 try
块一同使用。
示例:使用 try…catch 进行异常处理
public class Main {
public static void main(String[] args) {
try {
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
}
}
输出
ArithmeticException => / by zero
在这个例子中,我们试图将一个数字除以 0
。在这里,此代码生成一个异常。
为了处理异常,我们将代码 5 / 0
放在 try
块中。当发生异常时, try
块内的发生异常的代码以后的其余代码不会执行。
catch
块捕获异常并执行 catch
块内的语句。如果 try
块中的语句没有产生异常,则跳过 catch
块。
try…catch…finally
在 Java 中,try...catch
语句中可以根据需要添加一个 finally
块。无论是否有异常, finally
块总是被执行。
finally
块是可选的。而且每个 try
块只能有一个 finally
块。
finally
块的基本语法是:
try {
//code
}
catch (ExceptionType1 e1) {
// catch block
}
finally {
// finally block always executes
}
如果发生异常,则 finally
块在 try...catch
块之后执行。否则,它在 try
块之后执行。
示例:使用 finally 块的异常处理
public class Main {
public static void main(String[] args) {
try {
int divideByZero = 5 / 0;
} catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
} finally {
System.out.println("This is the finally block");
}
}
}
输出
ArithmeticException => / by zero
This is the finally block
在上面的示例中,我们在 try
块内将数字除以 0
,因此会生成一个 ArithmeticException
异常对象。
异常被 catch
块捕获,然后执行 catch
块中代码,最后执行 finally
块中的代码。
throw 关键字
throw
关键字用于显式抛出单个异常。当我们抛出一个异常时,程序的流程就进入了异常处理流程:要么被外部的 try...catch
块处理,要么程序因异常而终止。
示例:使用 Java throw 进行异常处理
public class Main {
public static void divideByZero() {
// throw an exception
throw new ArithmeticException("Trying to divide by 0");
}
public static void main(String[] args) {
divideByZero();
}
}
输出
Exception in thread "main" java.lang.ArithmeticException: Trying to divide by 0
at Main.divideByZero(Main.java:7)
at Main.main(Main.java:11)
在上面的例子中,我们明确地抛出 ArithmeticException
using throw
关键字。
throws 关键字
throws
关键字用于声明方法中可能发生的异常类型。它只是声明异常,而不是真正的处理异常。方法的使用者要根据方法上的异常声明处理异常,如果受检查的异常没有被处理,编译器就会给出编译提示。
示例:Java throws 关键字
import java.io.*;
public class Main {
// 声明异常
public static void findFile() throws IOException {
// 这里可能会产生异常
File newFile = new File("test.txt");
FileInputStream stream = new FileInputStream(newFile);
}
public static void main(String[] args) {
try {
findFile();
} catch (IOException e) {
System.out.println(e);
}
}
}
输出
java.io.FileNotFoundException: test.txt(系统找不到指定的文件)
当我们运行这个程序时,如果文件 test.txt 不存在,则 FileInputStream
抛出一个 FileNotFoundException
异常,它是 IOException
的子类。
findFile()
方法声明了可能会产生 IOException
异常,因此在 main()
方法中调用此方法时用了 try...catch
处理异常。
如果不想在 main()
方法中处理异常,则必须在 main()
方法上使用 throws
子句中声明异常。就像下面一样:
public static void main(String[] args) throws IOException {
findFile();
}
要了解更多信息,请访问 Java throw 和 throws 章节。