/**
* dispatch an uncaught exception to the handler. this method is
* intended to be called only by the jvm.
*/
private void dispatchuncaughtexception(throwable e) {
getuncaughtexceptionhandler().uncaughtexception(this, e);
}
/**
* returns the handler invoked when this thread abruptly terminates
* due to an uncaught exception. if this thread has not had an
* uncaught exception handler explicitly set then this thread's
* <tt>threadgroup</tt> object is returned, unless this thread
* has terminated, in which case <tt>null</tt> is returned.
* @since 1.5
* @return the uncaught exception handler for this thread
*/
public uncaughtexceptionhandler getuncaughtexceptionhandler() {
return uncaughtexceptionhandler != null ?
uncaughtexceptionhandler : group;
}
/**
* called by the java virtual machine when a thread in this
* thread group stops because of an uncaught exception, and the thread
* does not have a specific {@link thread.uncaughtexceptionhandler}
* installed.
* <p>
* the <code>uncaughtexception</code> method of
* <code>threadgroup</code> does the following:
* <ul>
* <li>if this thread group has a parent thread group, the
* <code>uncaughtexception</code> method of that parent is called
* with the same two arguments.
* <li>otherwise, this method checks to see if there is a
* {@linkplain thread#getdefaultuncaughtexceptionhandler default
* uncaught exception handler} installed, and if so, its
* <code>uncaughtexception</code> method is called with the same
* two arguments.
* <li>otherwise, this method determines if the <code>throwable</code>
* argument is an instance of {@link threaddeath}. if so, nothing
* special is done. otherwise, a message containing the
* thread's name, as returned from the thread's {@link
* thread#getname getname} method, and a stack backtrace,
* using the <code>throwable</code>'s {@link
* throwable#printstacktrace printstacktrace} method, is
* printed to the {@linkplain system#err standard error stream}.
* </ul>
* <p>
* applications can override this method in subclasses of
* <code>threadgroup</code> to provide alternative handling of
* uncaught exceptions.
*
* @param t the thread that is about to exit.
* @param e the uncaught exception.
* @since jdk1.0
*/
public void uncaughtexception(thread t, throwable e) {
if (parent != null) {
parent.uncaughtexception(t, e);
} else {
thread.uncaughtexceptionhandler ueh =
thread.getdefaultuncaughtexceptionhandler();
if (ueh != null) {
ueh.uncaughtexception(t, e);
} else if (!(e instanceof threaddeath)) {
system.err.print("exception in thread ""
+ t.getname() + "" ");
e.printstacktrace(system.err);
}
}
}
参考声明
how uncaught exceptions are handled 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对CodeAE代码之家的支持。
原文链接:https://droidyue.com/blog/2019/01/06/how-java-handle-uncaught-exceptions/