Does JVM throw exception from OS
I was asked this question which JVM throws Exception for OS if main throws? The JVM stops the application execution, but where will this exception be handled?
public static void main(String[] args) throws Exception {
display();
}
public static void display() throws Exception {
throw new RuntimeException();
}
If it can happen with any exception, please specify.
source to share
It is not handled by the OS, but by the JVM itself. From the Java Language Specification ยง11.3 :
If the catch clause cannot handle the exception, then the current thread (the thread that encountered the exception) exits
Your JVM will run with a single thread that will terminate. Then the JVM will close, according to JLS ยง12.8 (emphasis mine):
The program ends all of its activity and exits when one of two things happens:
- All threads that are not daemon threads terminate.
- Some threads call the exit method of the Runtime class or the System class, and the exit operation is not prohibited by the security manager.
This can happen with any type of exception at all. Note that in terms of the language specification, "exception" is actually java.lang.Throwable
any of its subclasses . This means it Error
also terminates threads / JVMs and everything that inherits from Throwable
, not just objects extending Exception
.
source to share