JDK-4158296 : need pluggable handler for unchecked exceptions on EventDispatchThread
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1,1.1.6
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,solaris_10
  • CPU: generic
  • Submitted: 1998-07-17
  • Updated: 1999-12-24
  • Resolved: 1999-12-24
Related Reports
Duplicate :  
Description
 Date: Thu, 16 Jul 1998 10:25:02 -0400
MIME-Version: 1.0
To: David Soroko <###@###.###>
CC: Ben Hutchison <###@###.###>,        "###@###.###" <###@###.###>,        ###@###.###
Subject: Re: Catching exceptions in AWT/Swing originated threads
Content-Transfer-Encoding: 7bit

David Soroko wrote:

> The problem is in the  EventDispatchThread class. Unbelievable as it is
> all unhandled exceptions originating from GUI are handled in the following
> way (jdk 1.1.6):
>         ............................
>         } catch (Throwable e) {
>                 System.err.println(
>                     "Exception occurred during event dispatching:");
>                 e.printStackTrace();
>             }
>         .......................

Horrendous isn't it.

This makes it a herculean task to handle unchecked exceptions (RuntimeExceptions and Errors) in a safe consistent way for an entire system.  I can't count all the times I've had to override processEvent to catch things just before
this code occurs.  "Luckily" we've already had to subclass almost every AWT widget class for one reason or another :-(

Count your blessings though.  The 1.1_Final only caught Exceptions not Throwables.  Errors took out the entire event thread.

I was hoping that this behavior would be fixed in newer releases to allow a "pluggable" handler.  Something like:

     // new public method in EventDispatchThread or Toolkit
     addUncaughtThrowableHandler(ThrowableHandler h);

     public Interface ThrowableHandler {
         public static final int RESUME;
         public static final int RETRY;
         public static final int EXIT;

         /**
          * called when an unhandled Throwable occurs
          * @return one of the above constants.
          * Of course this is only a suggestion to the caller.
          **/

         int handleThrowable(Throwable t);
     }


handleThrowable could be a void method for that matter.  The key is that you get notified about uncaught exceptions and can then deal with them in an appropriate manner.

Yes, I know you can handle this by creating your own ThreadGroup but you don't always have that option.

1.2b3 code looks just the same as the 1.1.6.  Guess I should have spoken up sooner...
--
Ralph M. Prescott               Systems Consultant
###@###.###                     Custom Development Services Division
###@###.###            LPA Software Inc.



Name: krT82822			Date: 05/01/99


Class java.awt.EventDispatchThread, in its run method,
catches Throwable, and prints it to stderr. As a result,
the caller application or applet never gets a chance to
catch the exception and sees output sent to stderr without
its consent.

Heres' the code of java.awt.EventDispatchThread:

    public void run() {
       while (doDispatch && !isInterrupted()) {
            try {
                AWTEvent event = theQueue.getNextEvent();
                theQueue.dispatchEvent(event);
            } catch (ThreadDeath death) {
                return;

	    } catch (InterruptedException interruptedException) {
		return; // AppContext.dispose() interrupts all
			// Threads in the AppContext

------->    } catch (Throwable e) {
                System.err.println(
                    "Exception occurred during event dispatching:");
                e.printStackTrace();
            }
        }
    }
(Review ID: 57608)
======================================================================

Comments
PUBLIC COMMENTS Need pluggable handler for unchecked exceptions on EventDispatchThread
10-06-2004

EVALUATION jeff.dunn@Eng 1998-08-17 This is a good idea, but we won't have the time to do it for 1.2.
17-08-1998