Home arrow static arrow Java Programming [Archive] - Thread
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Thread
This topic has 1 reply on 1 page.

Registered: 8/10/02
Aug 3, 2004 11:52 AM

If I get an Exception in the run() method of Thread then how can I capture that
(Something like divide by zero error) and will the system crash/will there be any memory out of Exception if a Thread runs in an infinite loop?


Registered: 9/26/01
Re: Thread  
Aug 3, 2004 11:58 AM (reply 1 of 1)

class MyThread extends Thread (or implements Runnable) {
private Exception exception;
public Exception getException() { return exception; }
public void run() {
try {
// do you stuff here. run() can't throw checked exception per contract
catch(SomeException e) {
exception = e;
catch(AnotherException e) {
exception = e;
yourThread.join(); // wait for the thread to finish
Exception e = yourThread.getException();
if (e != null) {
// your thread had ended in an exception. Do something with it here
This topic has 1 reply on 1 page.