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

(thread) setUncaughtExceptionHandler() doesn't work on main thread

XMLWordPrintable

    • b44
    • sparc
    • solaris_8

      JSR166 introduces a new method for specifying a handler to be invoked
      when a thread terminates with an uncaught exception. However setting
      the handler doesn't work on the main thread. This is because the
      launcher checks for an exception on return from the call to main()
      and explicitly prints a stack trace using the JNI ExceptionDescribe()
      function. Instead, it should just call the JNI function DetachCurrentThread().

      I expect the VM support for this feature to go into build 39 (see bugid 4986764)

      The following program demonstrates this bug:


      class exception3 extends Thread implements Thread.UncaughtExceptionHandler {

        int _val = 100;

        int c(int arr[]) {
          return arr[5] * 2;
        }

        int b(int arr[]) {
          return c(arr);
        }

        int a(int arr[]) {
          return b(arr);
        }

        int doit (int arr[]) {
          return a(arr);
        }

        public void run() {
          int arr[] = new int[2];
          int res = doit(arr);

          System.out.println(" result = " + res);
        }

        public void uncaughtException(Thread t, Throwable e) {
          if (e instanceof ArrayIndexOutOfBoundsException) {
            System.out.println("Pass.");
          } else {
            System.out.println("Fail: incorrect exception: " + e);
          }
        }

        static public void main(String args[]) {
          exception3 ex3 = new exception3();

          Thread.currentThread().setUncaughtExceptionHandler(ex3);

          ex3.run();
          System.out.println("Fail: exception not thrown.");
        }
      }

            martin Martin Buchholz
            sdeversunw Steve Dever (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: