Yes. An unhandled exception will kill the thread. When all non-daemon threads terminate your program will terminate. The exit status is non-zero if the last thread terminated with an exception. And your program has only one thread.
I am ducking the exception and let it pass on to JVM, shouldn't I get the compile time error?
-
27-11-2021 - |
문제
The sample code is:-
import javax.sound.midi.*;
import java.io.*;
class test{
public void go() throws MidiUnavailableException{
//try{
Sequencer sequencer = MidiSystem.getSequencer();
System.out.println("Got it");
//}
/*catch(Exception ex){
System.out.println("Size Matters");
}*/
/*catch(MidiUnavailableException ex){
System.out.println("I am the incorrect exception");
}*/
}
public static void main(String [] args) throws MidiUnavailableException{
test obj = new test();
//try{
obj.go();
//}
/*catch(MidiUnavailableException mex){
System.out.println("Compiler should catch me");
}*/
}
}
I don't get any while compiling the code; does that mean that JVM will handle the exception in the case? Or if the system is not able to give a sequencer then my program will terminate?
해결책
다른 팁
When your main method throws an exception, execution stops and the stacktrace is printed to stdout and the JVM shuts down.
There is nothing weird about this - it's acceptable to declare your main to throw exceptions, it's just that there's nothing to catch them, so all the JVM can do is explode.
Since main()
also throws MidiUnavailableException
, you are fine. If such an exception is thrown in go()
it will bubble up and stop the JVM.
제휴하지 않습니다 StackOverflow