【问题标题】:Proxying Thread.UncaughtExceptionHandler from Clojure从 Clojure 代理 Thread.UncaughtExceptionHandler
【发布时间】:2013-04-07 05:15:19
【问题描述】:

我正在尝试实现一个“完整的”未捕获异常处理程序,允许在 Clojure 中捕获 EDT 异常。

我正在尝试从此处接受的答案(超过 15 个赞成票)中实施课程:

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

这是我想移植到 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());

但是我被困住了。 UncaughtExceptionHandler 是在 Thread 中定义的公共接口,我似乎无法从 Clojure 代理它。

我不知道要导入什么,也不知道如何实现。

任何帮助将不胜感激,因为我似乎可以诊断出异常,因为它们在 EDT 的某个地方“丢失”(并且 EDT 会自动修复/重新启动)。

【问题讨论】:

    标签: swing exception clojure event-dispatch-thread


    【解决方案1】:

    使用reify 实现Thread$UncaughtExceptionHandler。 Java的内部类和接口被命名为类文件。

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

    见:Implementing Java generic interface in Clojure

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2012-05-06
      • 2011-08-23
      相关资源
      最近更新 更多