Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads and exceptions

 
Raymond Ong
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!