首页 > 解决方案 > 我可以编写 Error 对象以在 catch 块中捕获 Exception 对象吗

问题描述

编写捕获异常对象的捕获块和捕获错误对象的捕获块有什么区别?当我编写 Only Error catch 块时,它执行并捕获异常,结果与异常相同,但是当使用两个 catch 块编写时,它执行异常 catch 块而不是 catch 错误块。有人可以详细解释。

public class Main
{
    public static void main(String[] args) {
        int a=1,b=0;
        try{
            System.out.println(a/b);

        }catch(Error e){
            System.out.println("error"+e);
        }
         catch(Exceptione){
            System.out.println("exception"+e);
        }
    }
}

标签: javaexception

解决方案


推荐阅读