문제

우리는 애플리케이션 로그에서 이러한 예외에 대한 추적을 원합니다. 기본적으로 Java는 해당 예외를 콘솔에 출력합니다.

도움이 되었습니까?

해결책

EDT에서 포착되지 않는 예외와 EDT 외부에서 포착되지 않는 예외 사이에는 차이가 있습니다.

또 다른 질문에는 두 가지 모두에 대한 해결책이 있습니다 하지만 EDT 부분만 씹고 싶다면...

class AWTExceptionHandler {

  public void handle(Throwable t) {
    try {
      // insert your exception handling code here
      // or do nothing to make it go away
    } catch (Throwable t) {
      // don't let the exception get thrown out, will cause infinite looping!
    }
  }

  public static void registerExceptionHandler() {
    System.setProperty('sun.awt.exception.handler', AWTExceptionHandler.class.getName())
  }
}

다른 팁

Java 7부터는 다음과 같이 다르게 수행해야 합니다. sun.awt.exception.handler 해킹이 더 이상 작동하지 않습니다.

해결책은 다음과 같습니다. (에서 Java 7에서 포착되지 않은 AWT 예외).

// Regular Exception
Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());

// EDT Exception
SwingUtilities.invokeAndWait(new Runnable()
{
    public void run()
    {
        // We are in the event dispatching thread
        Thread.currentThread().setUncaughtExceptionHandler(new ExceptionHandler());
    }
});

약간의 추가 셈논답변:
EDT에서 발견되지 않은 RuntimeException(또는 오류)이 처음 발생하면 "sun.awt.Exception.handler" 속성을 찾고 해당 속성과 연결된 클래스를 로드하려고 시도합니다.EDT는 기본 생성자를 갖기 위해 Handler 클래스가 필요합니다. 그렇지 않으면 EDT는 이를 사용하지 않습니다.
처리 스토리에 좀 더 동적인 기능을 추가해야 하는 경우 클래스가 EDT에 의해 인스턴스화되어 정적 이외의 다른 리소스에 액세스할 기회가 없기 때문에 정적 작업을 사용하여 이를 수행해야 합니다.다음은 우리가 사용하고 있는 Swing 프레임워크의 예외 처리기 코드입니다.Java 1.4용으로 작성되었으며 그곳에서는 꽤 잘 작동했습니다.

public class AwtExceptionHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(AwtExceptionHandler.class);

    private static List exceptionHandlerList = new LinkedList();

    /**
     * WARNING: Don't change the signature of this method!
     */
    public void handle(Throwable throwable) {
        if (exceptionHandlerList.isEmpty()) {
            LOGGER.error("Uncatched Throwable detected", throwable);
        } else {
            delegate(new ExceptionEvent(throwable));
        }
    }

    private void delegate(ExceptionEvent event) {
        for (Iterator handlerIterator = exceptionHandlerList.iterator(); handlerIterator.hasNext();) {
            IExceptionHandler handler = (IExceptionHandler) handlerIterator.next();

            try {
                handler.handleException(event);
                if (event.isConsumed()) {
                    break;
                }
            } catch (Throwable e) {
                LOGGER.error("Error while running exception handler: " + handler, e);
            }
        }
    }

    public static void addErrorHandler(IExceptionHandler exceptionHandler) {
        exceptionHandlerList.add(exceptionHandler);
    }

    public static void removeErrorHandler(IExceptionHandler exceptionHandler) {
        exceptionHandlerList.remove(exceptionHandler);
    }

}

도움이 되길 바랍니다.

두 가지 방법이 있습니다:

  1. /* EDT에 Thread.UncaughtExceptionHandler 설치 */
  2. 시스템 속성을 설정합니다.System.setProperty("sun.awt.Exception.handler",MyExceptionHandler.class.getName());

후자가 SUN이 아닌 jvms에서 작동하는지 모르겠습니다.

--

실제로 첫 번째는 정확하지 않습니다. 이는 손상된 스레드를 감지하기 위한 메커니즘일 뿐입니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top