Forums Register Login

main() throws, who catches?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.
I didn't say it. I'm just telling you what this tiny ad said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 563 times.
Similar Threads
exception handling in initializers
try without catch
FileInputStream
NullPointerException
throwing exceptions
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:20:23.