Author Topic:   Uncaught exception
Ajith Kallambella
ranch hand
posted May 05, 2000 07:44 AM             
Hi folks,

This is a conceptual question.

Assume there are two user threads U1 and U2 that
a program spawns. Thread U1 throws an exception
which is not handled by the thread/program.
Which of the following is true -


a) U1 terminates and U2 continues to run.
b) U1 and U2 both terminate.
c) (b) and the JVM terminates too.

As I understand, uncaught exceptions
are handled by ThreadGroup.uncaughtException() which
terminates the program. But I am not very clear
how it affects the other threads that may
or may not be in the same thread group.

I really appreciate your inputs.


Ajith

Eric Barnhill
ranch hand
posted May 06, 2000 12:00 AM         
My understanding is that an uncaught exception causes the system to exit. Presumably that would take all the threads with it, if I have the right concept of system exit.

Eric

vishwas kumar bansal
unregistered
posted May 06, 2000 03:26 PM           
Exception must be caught somewhere.If a exception is throwned
by a thread u1,either it must be catch into thread u1 or it
must caught in the calling portion of u1.If the thread remains
uncaught and no throws statement is included in main of main
thread the exception is passed to jvm which terminate the main
thread and the corresponding threads.

maha anna
bartender
posted May 07, 2000 02:53 PM             
Ajith,
In order to convert your conceptual qstn into a practical one I wrote this foll prog. PLease cut and paste this code and analyze. It seems to me that If a program spawns user therad1 U1, and another user thread U2, then if user thread U1 throws an Exception, if it was not caught, the uncaughtException of the user thread U1's ThreadGroup is called, if that method does not catch this Exception, then it is propagated to this ThreadGroup's parent ThreadGroup's uncaughtException method and so on and finally if there is no one to care about then the JVN forms a UncaughtException object and gives to System.err which prints to our console. But the other User thread continues to do its work without any problem. And the JVM also DOES NOT EXIT because there is one more NO-PROBLEM user thread for it to care about.
regds
maha anna


class Question {
public static void main(String[] args) {

System.out.println("Starting User therad 1....");
Thread t1 = new MyThread1();
t1.start();

System.out.println("Starting User therad 2....");
Thread t2 = new MyThread2();
t2.start();

System.out.println("Last statement of Main(..) method");
}
}
class MyThread1 extends Thread {
public void run() {
for(int i=0; i<100; i++) {
try {
sleep(1000);
}catch(InterruptedException e){}
System.out.println(getName());
}
}
}

class MyThread2 extends Thread {
public void run() {
for(int i=0; i<100; i++) {
try {
sleep(1000);
}catch(InterruptedException e){}

if(i==3)
throw new RuntimeException();
System.out.println(getName());
}
}
}


[This message has been edited by maha anna (edited May 07, 2000).]

Eric Barnhill
ranch hand
posted May 07, 2000 10:05 PM         
My output here:
----
Starting therad 1....
Starting therad 2....
Last statement of main(..) method
Thread-1
Thread-2
Thread-1
Thread-2
Thread-1
Thread-2
java.lang.RuntimeException
at myThread2.run (Compiled Code)
Thread-1
Thread-1
Thread-1

[other 94 instances of "Thread-1" omitted]
----
great example, thanks!

|