• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

thread and exception!

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi! In the following program, after throwing the exception in main method, statement //1 in method run() always executes, why so? Shouldn't the program terminate after throwing the exception in main??
ashok.
_____________
public class Test extends Thread {
public void run() {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
System.err.println(e);
}
System.out.println("I am alive"); //1
}
public static void main(String args[]) {
new Test().start();
throw new NullPointerException();
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok..
Doesn't the program continue its course after successfully handling an exception? I believe the program would terminate only if the opposite happened.
This is an excerpt from Khalid Mughal's book.


On exit from a catch block, if there is any pending exception that has been thrown an not handled, the method is aborted and propagated as explained earlier (after any finally block has been executed). If the exception has been dealt with in a catch block, normal execution resumes. - Page:158.


Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 23, 2001).]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I'm not certain here, but this is what I think:
When you say "new Test().start()" you are creating a new thread. Now there are a total of 2 threads, your main thread (which is going to attempt to execute the remainder of the code in main) and your new thread (which will attempt to execute the code in the run() method). Now, after the line of code that creates the second thread, the main thread will throw an exception...BUT it is not up to the new thread to catch this exception. The main thread may therefore end with errors, but the new thread is still running. This thread will try sleeping for 300 milliseconds (easily accomplished without error) and then finish executing, printing out statement 1.
I hope this was clear (and right),
Matt
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok,
Try giving a return after the throw stmt in the main method
It wont compile
That means anything before throw alone gets executed
I know its weird to see the execution goes on after the exeception is thrown
The main reason is being the execution is not from the main thread instead its from the child thread which is spawned seperate
 
reply
    Bookmark Topic Watch Topic
  • New Topic