使用多个catch的Try中的Java断言
场景:使用多个catch的Try中的Java断言,java,exception-handling,assert,assertions,Java,Exception Handling,Assert,Assertions,场景: class Assert { public static void main(String []args) { try { assert false; } catch (RuntimeException re) { System.out.println("In the handler of RuntimeException"); } catch (Exc
class Assert {
public static void main(String []args) {
try {
assert false;
}
catch (RuntimeException re) {
System.out.println("In the handler of RuntimeException");
}
catch (Exception e) {
System.out.println("In the handler of Exception");
}
catch (Error ae) {
System.out.println("In the handler of Error");
}
catch (Throwable t) {
System.out.println("In the handler of Throwable");
}
}
}
我期望“在错误的处理程序中”,因为AssertionError是错误的子类,但它不显示任何内容并终止正常。然后,为了检查输出,我在错误处理程序之前添加了一个catch处理程序。catch (AssertionError t) {
System.out.println("In the handler of Throwable");
}
在“知道”中,捕捉错误不是一种好的做法,但如果我们不需要捕捉程序为何未崩溃,它将正常终止?默认情况下,当您使用
java
执行代码时,在命令行中添加-ea
:
java -ea Assert