无法捕获的ChuckNorrisException

本文翻译自:Uncatchable ChuckNorrisException

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable? 是否有可能在Java中构造一段代码,使得假设的java.lang.ChuckNorrisException无法捕获?

Thoughts that came to mind are using for example interceptors or aspect-oriented programming . 想到的想法是使用例如拦截器或面向方面的编程


#1楼

参考:https://stackoom.com/question/wFe2/无法捕获的ChuckNorrisException


#2楼

Any code can catch Throwable. 任何代码都可以捕获Throwable。 So no, whatever exception you create is going to be a subclass of Throwable and will be subject to being caught. 所以不,你创建的任何异常都将成为Throwable的子类,并且会被捕获。


#3楼

No. All exceptions in Java must subclass java.lang.Throwable , and although it may not be good practice, you can catch every type of exception like so: 不是.Java中的所有异常都必须是java.lang.Throwable子类,虽然它可能不是一个好习惯,但你可以像这样捕获每种类型的异常:

try {
    //Stuff
} catch ( Throwable T ){
    //Doesn't matter what it was, I caught it.
}

See the java.lang.Throwable documentation for more information. 有关更多信息,请参阅java.lang.Throwable文档。

If you're trying to avoid checked exceptions (ones that must be explicitly handled) then you will want to subclass Error, or RuntimeException. 如果您试图避免检查异常 (必须显式处理的异常 ),那么您将需要子类Error或RuntimeException。


#4楼

With such an exception it would obviously be mandatory to use a System.exit(Integer.MIN_VALUE); 有了这样的例外,显然必须使用System.exit(Integer.MIN_VALUE); from the constructor because this is what would happen if you threw such an exception ;) 来自构造函数,因为如果你抛出这样的异常会发生这种情况;)


#5楼

Any exception you throw has to extend Throwable, so it can be always caught. 您抛出的任何异常都必须扩展Throwable,因此可以始终捕获它。 So answer is no. 所以答案是否定的。

If you want to make it difficult to handle, you can override methods getCause(), getMessage() , getStackTrace() , toString() to throw another java.lang.ChuckNorrisException . 如果要使其难以处理,可以覆盖方法getCause(), getMessage()getStackTrace()toString()以抛出另一个java.lang.ChuckNorrisException


#6楼

I haven't tried this, so I don't know if the JVM would restrict something like this, but maybe you could compile code which throws ChuckNorrisException , but at runtime provide a class definition of ChuckNorrisException which does not extend Throwable . 我没有试过这个,所以我不知道JVM是否会限制这样的东西,但也许你可以编译抛出ChuckNorrisException代码,但是在运行时提供了ChuckNorrisException的类定义,它不会扩展Throwable

UPDATE: 更新:

It doesn't work. 它不起作用。 It generates a verifier error: 它会生成验证错误:

Exception in thread "main" java.lang.VerifyError: (class: TestThrow, method: ma\
in signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestThrow.  Program will exit.

UPDATE 2: 更新2:

Actually, you can get this to work if you disable the byte code verifier! 实际上,如果禁用字节码验证器,您可以使用它! ( -Xverify:none ) -Xverify:none

UPDATE 3: 更新3:

For those following from home, here is the full script: 对于那些在家的人,这里是完整的脚本:

Create the following classes: 创建以下类:

public class ChuckNorrisException
    extends RuntimeException // <- Comment out this line on second compilation
{
    public ChuckNorrisException() { }
}

public class TestVillain {
    public static void main(String[] args) {
        try {
            throw new ChuckNorrisException();
        }
        catch(Throwable t) {
            System.out.println("Gotcha!");
        }
        finally {
            System.out.println("The end.");
        }
    }
}

Compile classes: 编译类:

javac -cp . TestVillain.java ChuckNorrisException.java

Run: 跑:

java -cp . TestVillain
Gotcha!
The end.

Comment out "extends RuntimeException" and recompile ChuckNorrisException.java only : 注释掉“extends RuntimeException”并仅重新编译ChuckNorrisException.java

javac -cp . ChuckNorrisException.java

Run: 跑:

java -cp . TestVillain
Exception in thread "main" java.lang.VerifyError: (class: TestVillain, method: main signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestVillain.  Program will exit.

Run without verification: 运行时无需验证:

java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值