Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4388491

Provide a system wide exception handler to catch all uncaught exceptions

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.3.0
    • core-libs
    • unknown
    • generic

      At present, when an uncaught exception occurs in a thread it just prints the
      stack trace to System.err. Unfortunately, this is normally a hidden console
      that the user may never see and subsequently gets no feedback that an uncaught
      exception has occurred or that the thread has terminated. It would be a
      useful feature to have a pluggable, system wide uncaught exception handler.
      This would also allow the developer of an application to customize how all
      uncaught exceptions are handled. They could, for example, implement a bug
      reporting system that allowed the user to log a bug in their bugs database or
      send a bug report by e-mail to the developers. A system property could be used
      to set the default handler to use when one is not specified. This would allow
      the user to customize a handler for all applications when one is not explicitly
      set.

      I noticed that RFE 4304212 is a similar request to what I
      am proposing but this is specific to the AWT event dispatch thread only.

      Below I have detailed what I think would need to be changed to implement this and
      may be this will also give you a better idea of what I am proposing.

      Such a feature could be implemented with an interface to handle the uncaught
      exception:

      interface ThrowableHandler
      {
      void handleThrowable (Throwable t);
      }

      A DefaultThrowableHandler could be provided which implements the same
      functionality as present (i.e. printStackTrace):

      class DefaultThrowableHandler implements ThrowableHandler
      {
      public void handleThrowable (Throwable t)
      {
      t.printStackTrace();
      }
      }

      The System class could have some static methods to get/set the global handler:

      class System
      {
      private static ThrowableHandler throwableHandler;

      static
      {
      try
      {
      throwableHandler = Class.forName (System.getProperty
      ("java.lang.throwable.handler",
      "java.lang.DefaultThrowableHandler")).newInstance();

      }

      catch (Throwable t)
      {
      throwableHandler = new DefaultThrowableHandler ();
      }
      }

      public static ThrowableHandler getThrowableHandler () {return throwableHandler;}
      public static void setThrowableHandler (ThrowableHandler handler) {throwableHandler = handler;} .
      .
      .
      .

      }

      In the Thread class you would have a try/catch block around the whole run method
      to catch the uncaught exception and pass it on to the handler:

      class Thread
      {
      private void runThread ()
      {
      try
      {
      run ();
      }

      catch (Throwable t)
      {
      System.getThrowableHandler().handleThrowable (t);
      }
      }
      }

      The runThread method would be called by the start method instead of the run
      method directly thus allowing the capture of all uncaught exceptions from the
      run method.

      The AWT EventDispatchThread class could also be modified to do the same thing:

      class EventDispatchThread extends Thread
      {

        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:");
        // Change this to use System throwable handler
           //e.printStackTrace();
           System.getThrowableHandler().handleThrowable(e);
           }
           }
          }

      These changes would retain complete compatibility with older Java versions but
      just add some extra flexibility for both the developer and the user. I'm sure
      better names could be thought of for the new classes, interfaces and methods but
      the above is just an example of how this functionality could be
      implemented and the amount of work required.

      nicholas.allen@Ireland 2000-11-13

            jjb Josh Bloch
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: