public class ApplicationLoader extends ThreadGroup
{
private ApplicationLoader()
{
super("ApplicationLoader");
}
public static void main(String[] args)
{
Runnable appStarter = new Runnable()
{
public void run()
{
// invoke your application
// (i.e. MySystem.main(args)
System.out.println("ssss");
// throw new NullPointerException();
throw new Error("sssdsds");
}
};
new Thread(new ApplicationLoader(), appStarter).start();
}
// We overload this method from our parent
// ThreadGroup , which will make sure that it
// gets called when it needs to be. This is
// where the magic occurs.
public void uncaughtException(Thread thread, Throwable exception)
{
// Handle the error/exception.
// Typical operations might be displaying a
// useful dialog, writing to an event log, etc.
exception.printStackTrace();
}
}
这个是一个很好的异常处理机制。main中所有的错误都会转到uncaughtException方法中处理
本文介绍了一个Java应用程序中的异常处理机制,通过自定义ThreadGroup并覆盖uncaughtException方法来捕获main方法中的所有未捕获异常。
1403

被折叠的 条评论
为什么被折叠?



