ExceptionIOException 都是编译时检查异常。

但是,我们不能在 catch 块中使用 IOException。但是我们可以在 catch 块中使用 Exception 是什么原因。

    import java.io.*;
    class Demo{
        public static void main(String args[]){
            try{

            }catch(IOException e){ // Does not compile

            }

            try{

            }catch(Exception e){ // Compile

            }
        }
    }

最佳答案

除了 Exception (或 Throwable ),您无法捕获从未在 try 块中抛出的已检查异常。此行为由 JLS, Section 11.2.3 指定:

关于Java编译时检查异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32463601/

10-17 03:09