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.");
}
}
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.");
}
}
- relates to
-
JDK-4833089 (thread) If main thread dies from unhandled exception, isAlive() is still true
- Resolved