使用 throws 声明异常和使用 throw 抛出一个异常
在本教程中,我们将通过示例学习使用 throw 和 throws 关键字进行异常处理。
在本教程中,我们将通过示例学习使用 throw 和 throws 关键字进行异常处理。
在 Java 中,异常可以分为两种类型:
- 不受检查的异常: 它们不是在编译时检查,而是在运行时检查。例如:
RuntimeException
,ArithmeticException
,NullPointerException
,ArrayIndexOutOfBoundsException
,Error
类下的异常等。 - 受检查的异常: 它们在编译时被检查。例如
IOException
,InterruptedException
等。
请参阅 Java 异常以了解有关受检查的异常和不受检查的异常的详细信息。
要处理 Java 中的异常,除了使用 try...catch
语句捕获并处理之外,还可以使用 throws
在方法上声明异常或者通过 throw
抛出一个异常,将异常交给上游的程序处理。
本教程将介绍如何使用 throw
和 throws
处理异常。
Java throws 关键字
我们在方法声明中使用 throws
关键字来声明其中可能发生的异常类型。
它的语法是:
accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 … {
// code
}
从上面的语法可以看出,我们可以使用 throws
声明多个异常。
示例 1:Java throws 关键字
import java.io.*;
public class Main {
public static void findFile() throws IOException {
// 可能会产生 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 (No such file or directory)
当我们运行这个程序时,如果文件 test.txt
不存在,FileInputStream
会抛出一个 FileNotFoundException
异常, FileNotFoundException
是 IOException
的子类。
如果一个方法不处理异常,则必须在 throws
子句中指定其中可能发生的异常类型,以便调用堆栈中更靠上的方法可以处理它们。
findFile()
方法声明了可能会抛出 IOException
异常。在 main()
方法中调用此方法时必须使用 try...catch
捕获并处理IOException
异常,否则会有编译错误。
抛出多个异常
下面是我们如何使用 throws
关键字抛出多个异常。
import java.io.*;
public class Main {
public static void findFile() throws NullPointerException, IOException, InvalidClassException {
// 可能产生 NullPointerException 异常的代码
// 可能产生 IOException 异常的代码
// 可能产生 InvalidClassException 异常的代码
}
public static void main(String[] args) {
try{
findFile();
} catch(IOException e1){
System.out.println(e1.getMessage());
} catch(InvalidClassException e2){
System.out.println(e2.getMessage());
}
}
}
在这里, findFile()
方法使用 throws
声明了可能抛出 NullPointerException
, IOException
以及 InvalidClassException
。
请注意,没有处理 NullPointerException
. 这是因为它是一个未经检查的异常。没有必要在 throws
子句中指定并处理它。
throws 与 try…catch
throws
用于为方法声明可能会出现的异常。try...catch
用来捕获并处理异常。
有时候我们不需要立刻使用 try...catch
捕获并处理异常,而是将异常交给上游的方法进行处理,这个时候,就需要使用 throws
声明异常。
Java throw 关键字
throw
关键字用于显式抛出单个异常。抛出异常的目的是将异常的处理交给上游的程序,可以更加统一和灵活的处理程序。
抛出异常的语法是:
throw throwableObject;
throwableObject
是 Throwable
类或它的子类的实例。
示例 2:Java throw 关键字
public class Main {
public static void divideByZero() {
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:5)
at Main.main(Main.java:9)
在这个例子中,我们明确地抛出一个 ArithmeticException.
! 注意: ArithmeticException
是一个未经检查的异常。通常没有必要处理未经检查的异常。
示例 3:抛出受检查的异常
import java.io.*;
public class Main {
public static void findFile() throws IOException {
throw new IOException("File not found");
}
public static void main(String[] args) {
try {
findFile();
System.out.println("Rest of code in try block");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
输出
File not found
findFile()
方法抛出一个 IOException
异常的实例对象。
请注意,由于它是受检查的异常,因此我们必须在 throws
子句中指定它。调用 findFile()
方法需要处理此异常或使用 throws
关键字声明异常。
我们经在 main()
方法中处理了这个异常。当异常抛出时,程序执行流程从 try
块转移到 catch
块。因此, try
块中的其余代码将被跳过并执行 catch
块中的语句。