내가 찾고있는 것이 확실하지 않지만 스레드를 종료하는 예외 처리기가 있습니다. thread의 타겟으로 명시 적으로 캐치되지 않는 예외에 대한 핸들러입니다.
기본 "캐치되지 않는 예외 처리기"는 Throwable
에서 printStackTrace()
을 호출하여 스택 추적을 System.err
으로 인쇄합니다. 그러나, 당신이 할 수 replace log4j 대신에 예외를 기록 자신의 UncaughtExceptionHandler
이 :
class Log4jBackstop implements Thread.UncaughtExceptionHandler {
private static Logger log = Logger.getLogger(Log4jBackstop.class);
public void uncaughtException(Thread t, Throwable ex) {
log.error("Uncaught exception in thread: " + t.getName(), ex);
}
}
당신은 실행 프로그램 프레임 워크를 사용하는 경우가있는 당신이 Runnable
개체를 전달할 수는 그 스레드는 아마 않도록 자신의 catch
블록이 캐치되지 않는 예외 핸들러에 도달하는 예외가 있습니다. 당신이 거기 Runtime
예외를 포착하려면, 할 수있는 한 가지 방법은 다음과 같이 로깅 래퍼에서 각 작업을 래핑하는 것입니다 :
class Log4jWrapper {
private final Logger log;
private final Runnable target;
Log4jWrapper(Logger log, Runnable target) {
this.log = Objects.requireNonNull(log);
this.target = Objects.requireNonNull(target);
}
public void run() {
try {
target.run();
} catch(RuntimeException ex) {
log.error("Uncaught exception.", ex);
throw ex;
}
}
}
...
Runnable realTask = ...;
executor.submit(new Log4jWrapper(Logger.getLogger(Whatever.class), realTask));
당신이 "캐치되지 않는"들이 RuntimeException을 의미합니까? –
예 "잡히지 않음"을 의미합니다. RuntimeExceptions – DkS