2008-09-18 20 views

risposta

10

Esiste una distinzione tra le eccezioni non rilevate nell'EDT e all'esterno dell'EDT.

Another question has a solution for both ma se si desidera solo la parte EDT masticato ...

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

Non è necessario prendere il tiro. Non ci sarà il loop infinito. java.awt.EventDispatchThread.handleException sta rilevando qualsiasi eccezione per te. –

+0

Si dice che 'classi AWTExceptionHandler' –

0

Ci sono due modi:

  1. /* installare un Thread.UncaughtExceptionHandler sul EDT */
  2. Impostare una proprietà di sistema: System.setProperty ("sun.awt.exception.handler", MyExceptionHandler.class.getName());

Non so se quest'ultimo funziona su jvms non SUN.

-

Infatti, il primo non è corretta, è solo un meccanismo per rilevare un filo precipitato.

+1

Utilizzo di Thread.UncaufhtExceptionHandler non rileva le eccezioni EDT. La classe EDT cattura tutti i materiali da gettare e li stampa, piuttosto che lasciarli svolgere l'intero thread. – shemnon

+0

Mancano anche dettagli su ciò che è necessario nella seconda opzione, la classe MyExceptionHandler deve avere un metodo di istanza handle (Throwable) accessibile e un costruttore no-args accessibile. – shemnon

3

Un po 'Oltre a shemnon s Anwer:
la prima volta un RuntimeException non rilevata (o errore) si verifica nel EDT sta cercando la proprietà "sun.awt.exception.handler" e prova a caricare la classe associata alla proprietà. EDT richiede che la classe Handler abbia un costruttore predefinito, altrimenti l'EDT non lo userà.
Se è necessario portare un po 'più di dinamica nella storia di gestione, si è costretti a farlo con operazioni statiche, poiché la classe viene istanziata dall'EDT e quindi non ha possibilità di accedere ad altre risorse diverse da quelle statiche. Ecco il codice del gestore delle eccezioni dal nostro framework Swing che stiamo utilizzando. È stato scritto per Java 1.4 e ha funzionato abbastanza bene lì:

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); 
    } 

} 

Spero che sia d'aiuto.

10

Dal momento che Java 7, devi farlo in modo diverso come l'hack sun.awt.exception.handler non funziona più.

Here is the solution (da Uncaught 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()); 
    } 
}); 
Problemi correlati