• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Uncaught exception

 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 -
<PRE>
a) U1 terminates and U2 continues to run.
b) U1 and U2 both terminate.
c) (b) and the JVM terminates too.
</PRE>
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
 
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
<pre>

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());
}
}
}

</pre>

[This message has been edited by maha anna (edited May 07, 2000).]
 
Eric Barnhill
Rancher
Posts: 241
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic