• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Exception thrown by Thread

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would anyone explain why an uncaught exception thrown by one of the threads in a mulit-thread environment WILL NOT cause the program to terminate immediately?
Where, how and by what is the exception handled?
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,
It might help if you quoted your source or gave an example.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Enthuware Software Support
Posts: 4318
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A program has many threads (user and daemon).
2. When a thread encounters an uncaught exception, it dies (not the program). A thread is a part of the whole system. "It" is not the system.
3. If there are still user threads running, the program cannot terminate.
Of course, if the program has only one user thread and if that thread encounters an uncaught exception, the program will terminate.

------------------
Get Certified, Guaranteed!
www.enthuware.com/jqplus

SCJP2 Resources, WebCompiler, Compare Mock Exam Results and More!
www.jdiscuss.com
Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul. You cleared my wrong belief in that any uncaught exceptions will terminate the program. Perhaps this is due to the fact that most of the time I only ran the main thread and every uncaught exception in it cause it to stop.
As noted by Jane, I've here a program which can issue the situation for those interested:

The output is:
Starting Thread-0
Starting Thread-1
Main Thread ends!
Running Thread-0
Running Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
Thread-1
Thread-0
java.lang.RuntimeException at Test$2.run(Test.java:25)
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Process exit...

[This message has been edited by Kevin Yip (edited June 05, 2001).]
[This message has been edited by Kevin Yip (edited June 05, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!