• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main() throws, who catches?

 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I've got a main functions that throws some a generic exception, who catches it?

FileInputStream throws a FileNotFoundException but the program doesn't complain because main declares that it throws Exception. Of course, if I remove "throws Exception" then it says I need to catch the FileNotFoundException.
So if main throws the Exception, where is it being caught?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When there is no handler for an exception and it has propagated all the way up the calling stack, the thread in which it is thrown will be killed, and the ThreadGroup of which that thread is a member will handle the error. In your case, the main ThreadGroup catches the error and writes it to System.err, then the main thread dies; thus your program ends.
If you wanted to see how this works in another thread, just run your error-producing code in another thread. That thread will die, but your main will continue.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic