ilo*_*arn 3 java exception-handling exception
我有一个关于在Web应用程序中处理异常的问题.我经常听到抓住超级类异常是一个坏主意.
我经常编写代码来捕获struts action/java servlet类中的所有异常.
try {
// call business facade
// business facade calls DAO
// any exception from DAO bubbles up
} catch (Exception e) {
log.error("error", e);
}
Run Code Online (Sandbox Code Playgroud)
如果我们不捕获超类Exception.我们如何处理任何意外的运行时错误并适当地记录它们
您可以为项目设置一个DefaultUncaughtExceptionHandler处理未捕获异常的项目.例如,这是我在其中一个项目中的一段代码:
private static void setDefaultUncaughtExceptionHandler() {
try {
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
logger.error("Uncaught Exception detected in thread {}", t, e);
}
});
} catch (SecurityException e) {
logger.error("Could not set the Default Uncaught Exception Handler", e);
}
}
Run Code Online (Sandbox Code Playgroud)