File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Uncaught exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Uncaught exception" Watch "Uncaught exception" New topic
Author

Uncaught exception

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
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

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

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
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
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

Joined: Feb 25, 2000
Posts: 233

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uncaught exception
 
Similar Threads
2 questions about Threads
missing elements in array
JVM and Thread Help
Patrick Naughton and Threads
When does the JVM exit?