Java中的已检查与未检查异常

2021年3月10日15:59:56 发表评论 649 次浏览

在Java中, 有两种类型的异常:

1)已检查:是在编译时检查的异常。如果方法中的某些代码引发了检查异常, 那么该方法必须处理异常, 或者必须使用来指定异常抛出关键词。

例如, 考虑以下Java程序, 该程序在" C:\ test \ a.txt"位置打开文件并打印文件的前三行。该程序无法编译, 因为函数main()使用FileReader(), 而FileReader()引发了已检查的异常FileNotFoundException。它还使用readLine()和close()方法, 并且这些方法还会引发检查的异常IOException

import java.io.*;
  
class Main {
     public static void main(String[] args) {
         FileReader file = new FileReader( "C:\\test\\a.txt" );
         BufferedReader fileInput = new BufferedReader(file);
          
         // Print first 3 lines of file "C:\test\a.txt"
         for ( int counter = 0 ; counter < 3 ; counter++) 
             System.out.println(fileInput.readLine());
          
         fileInput.close();
     }
}

输出如下:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 
unreported exception java.io.FileNotFoundException; must be caught or declared to be 
thrown
    at Main.main(Main.java:5)

要修复上述程序, 我们要么需要使用throws指定异常列表, 要么需要使用try-catch块。我们在以下程序中使用了throws。以来FileNotFoundException是的子类IOException, 我们可以只指定IOException在throws列表中, 并使上面的程序无编译错误。

import java.io.*;
  
class Main {
     public static void main(String[] args) throws IOException {
         FileReader file = new FileReader( "C:\\test\\a.txt" );
         BufferedReader fileInput = new BufferedReader(file);
          
         // Print first 3 lines of file "C:\test\a.txt"
         for ( int counter = 0 ; counter < 3 ; counter++) 
             System.out.println(fileInput.readLine());
          
         fileInput.close();
     }
}

输出:文件" C:\ test \ a.txt"的前三行

2)未选中

是在编译时未检查的异常。在C ++中, 所有异常均未选中, 因此编译器不会强制其处理或指定异常。程序员要文明起来, 并指定或捕获异常。

在Java异常下

错误

RuntimeException

类是未经检查的异常, 对throwable下的所有其他内容都进行了检查。

+-----------+
           | Throwable |
                   +-----------+
                    /         \
           /           \
          +-------+          +-----------+
          | Error |          | Exception |
          +-------+          +-----------+
       /  |  \           / |        \
         \________/      \______/         \
                            +------------------+
    unchecked     checked    | RuntimeException |
                    +------------------+
                      /   |    |      \
                     \_________________/
                       
                       unchecked

考虑下面的Java程序。它可以编译, 但是会抛出ArithmeticException运行时。编译器允许它进行编译, 因为ArithmeticException是未经检查的异常。

class Main {
    public static void main(String args[]) {
       int x = 0 ;
       int y = 10 ;
       int z = y/x;
   }
}

输出如下:

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at Main.main(Main.java:5)
Java Result: 1

为什么是两种类型?

看到

未经检查的例外情况-争议

有关详细信息。

我们应该对异常进行检查还是取消检查?

以下是底线

Java文件

如果可以合理地期望客户端从异常中恢复, 请使其成为已检查的异常。如果客户端无法采取任何措施来从异常中恢复, 请将其设置为未经检查的异常

lsbin上的Java Corner

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: