2008-09-18 6 views

Répondre

10

Il existe une distinction entre les exceptions non interceptées dans l'EDT et en dehors de l'EDT.

Another question has a solution for both mais si vous voulez juste la partie HAE mâché ...

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

Pas besoin d'attraper jetable. Il n'y aura pas de boucle infinie. java.awt.EventDispatchThread.handleException capture toutes les exceptions pour vous. –

+0

Il est dit 'classs AWTExceptionHandler' –

0

Il y a deux façons:

  1. /* Installez un Thread.UncaughtExceptionHandler sur l'EDT */
  2. Définir une propriété système: System.setProperty ("sun.awt.exception.handler", MyExceptionHandler.class.getName());

Je ne sais pas si ce dernier fonctionne sur des jvms non SUN.

-

En effet, le premier n'est pas correct, il est seulement un mécanisme de détection d'un fil écrasé.

+1

L'utilisation de Thread.UncaufhtExceptionHandler n'attrape pas les exceptions EDT. La classe EDT attrape tous les jetables et les imprime plutôt que de les laisser dérouler tout le fil. – shemnon

+0

Il vous manque également des détails sur ce qui est nécessaire dans la deuxième option, la classe MyExceptionHandler doit avoir une méthode d'instance handle (Throwable) accessible et un constructeur no-args accessible. – shemnon

3

Un peu plus de shemnon de la anwer:
La première fois une RuntimeException uncaught (ou erreur) se produit dans la EDT recherche la propriété "sun.awt.exception.handler" et tente de charger la classe associée à la propriété. EDT a besoin de la classe Handler pour avoir un constructeur par défaut, sinon EDT ne l'utilisera pas.
Si vous devez apporter un peu plus de dynamique dans l'histoire de la manipulation, vous devez le faire avec des opérations statiques, car la classe est instanciée par l'EDT et n'a donc aucune chance d'accéder à d'autres ressources statiques. Voici le code du gestionnaire d'exception de notre framework Swing que nous utilisons. Il a été écrit pour Java 1.4 et il a fonctionné assez bien 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); 
    } 

} 

Espérons que cela aide.

10

Depuis Java 7, vous devez le faire différemment car le hack sun.awt.exception.handler ne fonctionne plus.

Here is the solution (à partir de 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()); 
    } 
}); 
Questions connexes