java多線程編程之捕獲子線程異常示例
通過try catch是無法捕獲子線程異常的,Thread對象提供了setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)方法用來獲取線程中產(chǎn)生的異常。
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");
}
}
}
相關(guān)文章
配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10SpringCloud遠(yuǎn)程服務(wù)調(diào)用三種方式及原理
本文給大家介紹SpringCloud遠(yuǎn)程服務(wù)調(diào)用實(shí)戰(zhàn)筆記,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-12-12spring @schedule注解如何動態(tài)配置時(shí)間間隔
這篇文章主要介紹了spring @schedule注解如何動態(tài)配置時(shí)間間隔,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的
這篇文章主要介紹了Mybatis中的mapper是如何和XMl關(guān)聯(lián)起來的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Spring?@bean和@component注解區(qū)別
本文主要介紹了Spring?@bean和@component注解區(qū)別,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01