Java throw和throws 关键字

Bertha 。 2024-03-22 23:45 175阅读 0赞

07b95a2ddcbe4a03a5c54df5c6eaf214.jpeg

在Java中,异常可以分为两种类型:

  • 未检查的异常:它们不是在编译时而是在运行时被检查,例如:ArithmeticException,NullPointerException,ArrayIndexOutOfBoundsException,Error类下的异常等。
  • 检查的异常:在编译时检查它们。例如IOException,InterruptedException等。

通常,我们不需要处理未经检查的异常。这是因为由于编程错误而发生了未经检查的异常。并且,纠正它们而不是处理它们是一个好习惯。

throws 关键字

我们在方法声明中使用throws关键字来声明其中可能发生的异常的类型。

其语法为:

  1. accessModifier returnType methodName() throws ExceptionType1, ExceptionType2 {
  2. // code
  3. }

从上面的语法可以看到,我们可以用throws来声明多个异常。

示例1:Java throws 关键字

  1. import java.io.*;
  2. class Main {
  3. public static void findFile() throws IOException {
  4. //可能产生IOException的代码
  5. File newFile=new File("test.txt");
  6. FileInputStream stream=new FileInputStream(newFile);
  7. }
  8. public static void main(String[] args) {
  9. try{
  10. findFile();
  11. } catch(IOException e){
  12. System.out.println(e);
  13. }
  14. }
  15. }

输出结果

  1. java.io.FileNotFoundException: test.txt (No such file or directory)

当我们运行这个程序时,如果文件test.txt不存在,FileInputStream将抛出一个继承IOException类的FileNotFoundException异常。

如果方法不处理异常,则必须在throws子句中指定该方法中可能发生的异常类型,以便调用堆栈中更高层的方法可以处理它们或使用throws关键字本身指定它们。

findFile()方法指定可以抛出IOException。 main()方法调用此方法并处理抛出的异常。

引发多个异常

这是我们如何使用throws关键字引发多个异常的方法。

  1. import java.io.*;
  2. class Main {
  3. public static void findFile() throws NullPointerException, IOException, InvalidClassException {
  4. // 可能产生NullPointerException的代码
  5. // 可能产生IOException的代码
  6. // 可能产生InvalidClassException的代码
  7. }
  8. public static void main(String[] args) {
  9. try{
  10. findFile();
  11. } catch(IOException e1){
  12. System.out.println(e1.getMessage());
  13. } catch(InvalidClassException e2){
  14. System.out.println(e2.getMessage());
  15. }
  16. }
  17. }

这里,findFile()方法指定它可以在其throws子句中抛出NullPointerException、IOException和InvalidClassException。

请注意,我们尚未处理NullPointerException。这是因为它是未经检查的异常。不必在throws子句中指定它并进行处理。

throws 关键字 与try…catch…finally

可能有几个方法会导致异常。为每种方法编写try…catch将是乏味的,并且代码将变得冗长且难以理解。

当您已检查了不希望在当前方法中捕获的异常(必须处理的异常)时,throws也很有用。

throw关键字

throw关键字用于显式地抛出一个异常。

当引发异常时,程序执行的流程从try块转移到catch块。我们在方法中使用throw关键字。

其语法为:

  1. throw throwableObject;

Throwable对象是Throwable类或Throwable类的子类的实例。

示例2:Java throw关键字

  1. class Main {
  2. public static void divideByZero() {
  3. throw new ArithmeticException("试图除以0");
  4. }
  5. public static void main(String[] args) {
  6. divideByZero();
  7. }
  8. }

输出结果

  1. Exception in thread "main" java.lang.ArithmeticException: 试图除以0
  2. at Main.divideByZero(Main.java:3)
  3. at Main.main(Main.java:7)
  4. exit status 1

在此示例中,我们明确抛出 ArithmeticException.

注意: ArithmeticException是未经检查的异常。通常没有必要处理未经检查的异常。

示例3:抛出检查异常

  1. import java.io.*;
  2. class Main {
  3. public static void findFile() throws IOException {
  4. throw new IOException("文件未找到");
  5. }
  6. public static void main(String[] args) {
  7. try {
  8. findFile();
  9. System.out.println("try块中的其余代码");
  10. } catch (IOException e) {
  11. System.out.println(e.getMessage());
  12. }
  13. }
  14. }

输出结果

  1. 文件未找到

findFile()方法使用传递给其构造函数的消息抛出一个IOException。

注意,由于它是一个检查的异常,因此必须在throws子句中指定它。

调用findFile()方法的方法需要处理此异常,或者自己使用throws关键字指定它。

我们已经在main()方法中处理了此异常。引发异常时,程序执行的流程在try块之间转移到catch。因此,将跳过该try块中的其余代码,并执行该catch块中的语句。

发表评论

表情:
评论列表 (有 0 条评论,175人围观)

还没有评论,来说两句吧...

相关阅读

    相关 throw throws 关键字

    1.系统自动抛出异常: 当程序中有逻辑错误、类型转换错误时,系统自动抛出异常,例如: ![70][] ![70 1][]  2 .throw 关键字抛出异常 ![