java多線程編程之捕獲子線程異常示例
通過try catch是無法捕獲子線程異常的,Thread對象提供了setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)方法用來獲取線程中產生的異常。
package threads;
import java.lang.Thread.UncaughtExceptionHandler;
public class TextException
{
public static void main(String[] args)
{
Test test = new Test();
test.setUncaughtExceptionHandler(new UncaughtExceptionHandler()
{
public void uncaughtException(Thread t, Throwable e)
{
System.out.println(t.getName() + " : " + e.getMessage());
// TODO
}
});
}
public static class Test extends Thread
{
public Test()
{
}
public void run()
{
throw new RuntimeException("just a test");
}
}
}
相關文章
springboot的SpringPropertyAction事務屬性源碼解讀
這篇文章主要介紹了springboot的SpringPropertyAction事務屬性源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
Java 配置log4j日志文件路徑 (附-獲取當前類路徑的多種操作)
這篇文章主要介紹了Java 配置log4j日志文件路徑 (附-獲取當前類路徑的多種操作),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10

