corda/sgx-jvm/avian/test/ThreadExceptions.java
Chris Rankin c0c75c3e19 Squashed commit of the following: (#32)
commit a25c09bb738e7e82d2dfd909381ba052f7b69687
Merge: aa6664214 92af5d169
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:38:08 2017 -0600

    Merge pull request #545 from corda/chrisr3-uncaught-exceptions

    Support Thread.uncaughtExceptionHandler with OpenJDK

commit aa666421499d0e9ed46b6c745a93abd9998a58b0
Merge: dc8c99bd2 1cb11e964
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:23:41 2017 -0600

    Merge pull request #546 from corda/chrisr3-werror

    Fix "fallthrough" warnings with recent GCC.

commit dc8c99bd2f045c787f2c322cd65f866626fa9461
Author: Joel Dice <joel.dice@gmail.com>
Date:   Tue Sep 5 11:17:54 2017 -0600

    fix bootimage-test build regression

commit 1cb11e964fab925e9d98373ba58e181b1a1f11fd
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Tue Sep 5 10:25:48 2017 +0100

    Fix "fallthrough" warnings with recent GCC.

commit 92af5d169dbfb7b9b9f5b667b0dc71ca7b98b416
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Mon Sep 4 17:26:31 2017 +0100

    Ensure that the thread resources are only cleaned up once.

commit 05d260f8bed68e3f999619aee338a781ba0f4c63
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Mon Sep 4 17:10:04 2017 +0100

    Test exception thrown from uncaught-exception handler.

commit b1c5dca36163a876fba86221493883ddbd5fe805
Author: Chris Rankin <chris.rankin@r3.com>
Date:   Tue Aug 29 17:17:24 2017 +0100

    Support Thread.uncaughtExceptionHandler.
2017-09-06 10:48:26 +01:00

72 lines
1.8 KiB
Java

public class ThreadExceptions {
private static void expect(boolean v) {
if (! v) throw new RuntimeException("Expectation failed");
}
private static class Handler implements Thread.UncaughtExceptionHandler {
public String message;
@Override
public void uncaughtException(Thread t, Throwable e) {
message = e.getMessage();
}
}
public static void main(String[] args) throws Exception {
{ Thread thread = new Thread() {
@Override
public void run() {
throw new RuntimeException("TEST-DEFAULT-HANDLER");
}
};
Handler handler = new Handler();
Thread.setDefaultUncaughtExceptionHandler(handler);
thread.start();
thread.join();
expect("TEST-DEFAULT-HANDLER".equals(handler.message));
}
Thread.setDefaultUncaughtExceptionHandler(null);
{ Thread thread = new Thread() {
@Override
public void run() {
throw new RuntimeException("TEST-HANDLER");
}
};
Handler handler = new Handler();
thread.setUncaughtExceptionHandler(handler);
thread.start();
thread.join();
expect("TEST-HANDLER".equals(handler.message));
}
{ Thread thread = new Thread() {
@Override
public void run() {
throw new RuntimeException("TEST-BAD-HANDLER");
}
};
Handler handler = new Handler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
super.uncaughtException(t, e);
throw new IllegalStateException("BAD THING");
}
};
thread.setUncaughtExceptionHandler(handler);
thread.start();
thread.join();
expect("TEST-BAD-HANDLER".equals(handler.message));
System.out.println("Exception from UncaughtExceptionHandler was ignored");
}
}
}