是否有可能在Java中构造一段代码,使假设的Java .lang. chucknorrisexception无法捕获?
我想到的是使用拦截器或面向方面的编程。
是否有可能在Java中构造一段代码,使假设的Java .lang. chucknorrisexception无法捕获?
我想到的是使用拦截器或面向方面的编程。
当前回答
public class ChuckNorrisException extends Exception {
public ChuckNorrisException() {
System.exit(1);
}
}
(当然,从技术上讲,这个异常永远不会被真正抛出,但是一个正确的ChuckNorrisException不能被抛出——它会先抛出你。)
其他回答
你可以把ChuckNorris隐藏起来,也可以把他封装起来,也可以把他吞下去……
try {doChuckAction();} catch(ChuckNorrisException cne){/*做其他事情*/}
Two fundamental problems with exception handling in Java are that it uses the type of an exception to indicate whether action should be taken based upon it, and that anything which takes action based upon an exception (i.e. "catch"es it) is presumed to resolve the underlying condition. It would be useful to have a means by which an exception object could decide which handlers should execute, and whether the handlers that have executed so far have cleaned things up enough for the present method to satisfy its exit conditions. While this could be used to make "uncatchable" exceptions, two bigger uses would be to (1) make exceptions which will only be considered handled when they're caught by code that actually knows how to deal with them, and (2) allow for sensible handling of exceptions which occur in a finally block (if a FooException during a finally block during the unwinding of a BarException, both exceptions should propagate up the call stack; both should be catchable, but unwinding should continue until both have been caught). Unfortunately, I don't think there would be any way to make existing exception-handling code work that way without breaking things.
在构造函数中,可以启动一个反复调用originalThread的线程。停止(ChuckNorisException.this)
线程可以重复捕获异常,但会一直抛出异常,直到异常死亡。
在finalize中调用System.exit(1),并抛出来自所有其他方法的异常副本,以便程序退出。
在这种例外情况下,显然必须使用System.exit(Integer.MIN_VALUE);从构造函数中,因为如果抛出这样的异常,就会发生这种情况;)