Reputation: 8985
One of my multi-threaded applications does not include the thread name when it throws an exception. I get exceptions like below
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at dataimporter.Importer.run(Importer.java:201)
at java.lang.Thread.run(Unknown Source)
Is there anyway for it to include the thread name?
Upvotes: 2
Views: 141
Reputation: 279950
You can register an UncaughtExceptionHandler
with your Thread
and print out the information you need.
Thread thread = new Thread(..);
thread.setUncaughtExceptionHandler((t, ex) -> // fancy Java 8 syntax
System.out.println(t.getName() + " " + ex)
); // or print the stack trace after it
thread.start();
The above has some Java 8 syntax. If you aren't using that, you can simply use an anonymous class.
UncaughtExceptionHandler handler = new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable ex) {
System.out.println(t.getName() + " " + ex);
}
};
thread.setUncaughtExceptionHandler(handler);
Upvotes: 4