本文共 624 字,大约阅读时间需要 2 分钟。
Can someone please explain the reasoning behind it?
是的,因为JVM会小心忽略您在run方法中抛出的任何异常.因此,抛出它可能是一个错误(除非你有线程的特定异常处理程序,请参阅the docs).没有理由煽动潜在的错误行为.
或者,举个例子.
class MyThread extends Thread {
public void run() {
throw new RuntimeException();
}
}
...
new MyThread().start();
// here thread dies silently with no visible effects at all
编辑
Why can’t the parent thread ‘catch’ the exception from the spawned ‘child’ thread?
@ chaotic3quilibrium在他的评论中已经注意到为什么不:因为父线程已经可能已经移动了.
new MyThread().start(); // launch thread and forget
// 1000 lines of code further...
i = i + 1; // would you like exception from child thread to be propagated here?
转载地址:http://cqudv.baihongyu.com/