2

I'm trying to implement a "complete" uncaught exception handler, allowing to also catch EDT exceptions in Clojure.

I'm trying to implement the class from the accepted answer (with 15+ upvotes) from here:

How can i catch Event Dispatch Thread (EDT) exceptions?

Here's the part that I'd like to port to Clojure:

public static class ExceptionHandler implements Thread.UncaughtExceptionHandler {

    public void handle(Throwable thrown) {
      // for EDT exceptions
      handleException(Thread.currentThread().getName(), thrown);
    }

    public void uncaughtException(Thread thread, Throwable thrown) {
      // for other uncaught exceptions
      handleException(thread.getName(), thrown);
    }

    protected void handleException(String tname, Throwable thrown) {
      System.err.println("Exception on " + tname);
      thrown.printStackTrace();
    }
  }

  public static void main(String[] args) {
    Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
    System.setProperty("sun.awt.exception.handler",
                       ExceptionHandler.class.getName());

However I'm stuck. The UncaughtExceptionHandler is a public interface defined inside Thread and I can't seem to proxy it from Clojure.

I don't know what to import nor how to implement this.

Any help would be greatly appreciated because I'm having exceptions that I can seem to diagnose because they're "lost" somewhere in the EDT (and the EDT is automatically fixed / restarted).

Community
  • 1
  • 1
Cedric Martin
  • 5,945
  • 4
  • 34
  • 66

1 Answers1

1

Use reify to implement Thread$UncaughtExceptionHandler. Inner classes and interfaces from Java are named as the class file.

(def h (reify Thread$UncaughtExceptionHandler    
  (uncaughtException [this t e] 
    (println t ": " e))))  

See: Implementing Java generic interface in Clojure

Community
  • 1
  • 1
noahlz
  • 10,202
  • 7
  • 56
  • 75