• 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

Threads and exceptions

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a single threaded application. What happens when a checked exception is thrown? I've read that the JVM will search for an exception handler for the thrown exception. Let's say that the handler is finally found in the main() method. What happens next? Will my application continue to the next line after the exception handler? Likewise, what happens when an unchecked exception is caught? Also, if I have a multi-threaded application and an unchecked or a checked exception is caught, what will happen to the thread where the exception was caught? Will the other threads be affected? Is there any behavioral difference if an unchecked or a check exception is caught in a multi-threaded application?

Also, if there are resources regarding my questions. Please post the links and I'd be glad to read 'em up.

Thanks!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


What happens when a checked exception is thrown? I've read that the JVM will search for an exception handler for the thrown exception. Let's say that the handler is finally found in the main() method. What happens next? Will my application continue to the next line after the exception handler?



The same thing happens whether it's checked or unchecked, main or anywhere else. In a thread or "not in a thread." (That last is in quotes because eveything in Java is in a thread.)

The exception bubbles up until it's caught. If the catch block or any following finally doesn't throw or return, then execution continues after the catch.

If nobody handles it up to the run() method of that thread's Runnable (yes, main is called from a Runnable's run() method, I think), then that thread's ThreadGroup's uncaughtException is invoked. By default, I think this just prints out a stack trace.

That thread dies, but others continue.


Also, if there are resources regarding my questions. Please post the links and I'd be glad to read 'em up.



Thread tutorial
Exception tutorial
 
reply
    Bookmark Topic Watch Topic
  • New Topic