Java 抛出和抛出
Java 抛出和抛出
在本教程中,我们将通过示例学习使用 throw 和 throws 关键字进行异常处理。
在Java中,异常可以分为两类:
- 未经检查的异常: 它们不是在编译时而是在运行时检查的。例如:
ArithmeticException
,NullPointerException
,ArrayIndexOutOfBoundsException
,Error
下的异常 类等。 - 已检查的异常: 它们在编译时被检查。例如,
IOException
,InterruptedException
等。
参考Java Exceptions详细了解checked和unchecked exceptions。
通常,我们不需要处理未经检查的异常。这是因为由于编程错误而发生未经检查的异常。而且,纠正它们而不是处理它们是一个好习惯。
本教程现在将重点介绍如何使用 throw
处理已检查的异常 和 throws
.
Java throws 关键字
我们使用 throws
方法声明中的关键字来声明其中可能发生的异常类型。
它的语法是:
accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 … {
// code
}
从上面的语法可以看出,我们可以使用 throws
声明多个异常。
示例 1:Java 抛出关键字
import java.io.*;
class Main {
public static void findFile() throws IOException {
// code that may produce 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
它扩展了 IOException
类。
如果一个方法不处理异常,则必须在throws
中指定其中可能发生的异常类型 子句,以便调用堆栈中更靠前的方法可以处理它们或使用 throws
指定它们 关键字自己。
findFile()
方法指定 IOException
可以扔。 main()
方法调用该方法,如果抛出异常则处理。
抛出多个异常
以下是我们如何使用 throws
引发多个异常 关键字。
import java.io.*;
class Main {
public static void findFile() throws NullPointerException, IOException, InvalidClassException {
// code that may produce NullPointerException
… … …
// code that may produce IOException
… … …
// code that may produce 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()
方法指定它可以抛出 NullPointerException
, IOException
, 和 InvalidClassException
在其 throws
子句。
请注意,我们还没有处理 NullPointerException
.这是因为它是一个未经检查的异常。不需要在throws
中指定 子句并处理它。
抛出关键字 Vs.尝试...抓住...终于
可能有几种方法会导致异常。编写 try...catch
因为每种方法都会很乏味,代码也会变得冗长且可读性差。
throws
当您检查了不想在当前方法中捕获的异常(必须处理的异常)时也很有用。
Java throw 关键字
throw
关键字用于显式抛出单个异常。
当抛出异常时,程序执行流程从try
转移 阻止到 catch
堵塞。我们使用 throw
方法中的关键字。
它的语法是:
throw throwableObject;
可抛出对象是类 Throwable
的实例 或 Throwable
的子类 类。
示例 2:Java throw 关键字
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:3) at Main.main(Main.java:7) exit status 1
在这个例子中,我们明确地抛出了一个 ArithmeticException.
注意: ArithmeticException
是未经检查的异常。通常不需要处理未经检查的异常。
示例 3:抛出已检查异常
import java.io.*;
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
中的语句 块被执行。
java