2008-09-18 12 views

答えて

10

EDT内とEDT外で、キャッチされない例外が区別されます。

Another question has a solution for bothあなただけ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()) 
    } 
} 
+2

throwableをキャッチする必要はありません。無限ループはありません。 java.awt.EventDispatchThread.handleExceptionが例外をキャッチしています。 –

+0

そこに 'クラスAWTExceptionHandler' –

0

2つの方法があります。

  1. /* EDT上のThread.UncaughtExceptionHandlerをインストールします*/
  2. システムプロパティを設定: するSystem.setProperty( "sun.awt.exception.handler" は、 MyExceptionHandler.class.getName());

後者が非Sunのjvmsで動作するかどうかはわかりません。

-

確かに、最初はそれがクラッシュしたスレッドを検出するための唯一の手段だ、正しくありません。

+1

Using Thread.UncaufhtExceptionHandlerはEDT例外をキャッチしません。 EDTクラスはすべてのスローアブルをキャッチし、スレッド全体を巻き戻すのではなく、スローバイアブルを出力します。 – shemnon

+0

2番目のオプションで必要なものについての詳細もわかりません。MyExceptionHandlerクラスには、ハンドル(Throwable)インスタンスメソッドがアクセス可能で、no-argsコンストラクターがアクセス可能でなければなりません。 – shemnon

3

shemnonのanwerに少し加え:
初めてキャッチされないのRuntimeException(またはエラー)で発生EDTはプロパティ "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); 
    } 

} 

希望します。

10

Java 7以降では、sun.awt.exception.handlerハックがもう機能しないため、別の方法で行う必要があります。

Here is the solutionUncaught AWT Exceptions in Java 7)。

// 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()); 
    } 
}); 
関連する問題