[英]Why is UncaughtExceptionHandler not called by ExecutorService?
[英]UncaughtExceptionHandler not called
我在Groovy / Java中使用UncaughtExceptionHandler
遇到麻烦。
class UncaughtExceptionLogger implements Thread.UncaughtExceptionHandler {
@Override
void uncaughtException(Thread t, Throwable e) {
//TODO do some logging;
println "test";
}
main..groovy
def main(){
def handler = new UncaughtExceptionLogger();
Thread.defaultUncaughtExceptionHandler = handler
String s;
s.charAt(10); // causes a NullPointerException but the exception handler is not called
}
main();
我为什么期望抛出NullPointerException
时被调用的异常处理程序,但是这种情况不会发生。 我究竟做错了什么?
似乎必须使用单独的线程生成它:
class UncaughtExceptionLogger implements Thread.UncaughtExceptionHandler {
@Override
void uncaughtException(Thread t, Throwable e) {
//TODO do some logging;
println "test";
}
}
def main(){
Thread.defaultUncaughtExceptionHandler = new UncaughtExceptionLogger()
String s;
s.charAt(10); // causes a NullPointerException but the exception handler is not called
}
Thread.start {
main()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.