"Handle or declare. That's the law." - Head First
But, is it a good law? Let me give an example first:
public static void main(String[] args) throws Exception {
m1();
}
static void m1() throws Exception{
m2();
}
static void m2() throws Exception {
throw new Exception();
}
m2()
throws exception and m1()
calls m2()
, meaning it must either handle or declare it. Hmmm let's declare it. Then main()
calls m1()
and it has same poll: declare or handle. I again decided to declare it and code compiles just fine.
Okay, it works, but who handled this exception at all? Looks like no one did. I know I am a beginner, but I don't like the sound of that. Yes, some methods can decide whether to declare or handle exceptions, but why main()
? Shouldn't main method be one that just handles? In that way, no exception could "slip".
Am I missing something to this? I was honestly surprised that it is okay for main method to just declare exception, knowing that it is the last place we could technically catch something.
The main method is designed to catch and handle all types of exceptions.Is incorrect as JVM doesn't handle unchecked exceptions.
When exception is thrown by main() method, Java Runtime terminates the program and print the exception message and stack trace in system console. The throws clause only states that the method throws a checked FileNotFoundException and the calling method should catch or rethrow it.
The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code. The throws keyword is used in a method signature and declares which exceptions can be thrown from a method.
The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.
who handled this exception at all?
The Java runtime did.
More specifically, the UncaughtExceptionHandler
did, as specified in the Java Language Specification (JLS), section 11.3. Run-Time Handling of an Exception:
If no
catch
clause that can handle an exception can be found, then the current thread (the thread that encountered the exception) is terminated. Before termination, allfinally
clauses are executed and the uncaught exception is handled according to the following rules:
If the current thread has an uncaught exception handler set, then that handler is executed.
Otherwise, the method
uncaughtException
is invoked for theThreadGroup
that is the parent of the current thread. If theThreadGroup
and its parentThreadGroup
s do not overrideuncaughtException
, then the default handler'suncaughtException
method is invoked.
So, by default, when main()
throws an exception, unchecked or checked, the built-in default "uncaught exception handler" will simply print the stacktrace to System.err
, as-if the following was executed right before main()
was called:
Thread.setDefaultUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler());
class DefaultUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
@Override
public void uncaughtException(Thread t, Throwable e) {
e.printStackTrace();
}
}
After the "uncaught exception handler" has been invoked the thread is terminated, same as if you simply return from the main()
method, and unless the code has started non-daemon threads that are still running, the program ends.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With