0

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?

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92
Ankit
  • 609
  • 2
  • 10
  • 26

3 Answers3

2

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • *"... so all the JVM can do is explode."*. Erm ... it generally exits with a stacktrace and no actual explosions :-) – Stephen C Nov 06 '12 at 13:22
2

Since main() also throws MidiUnavailableException, you are fine. If such an exception is thrown in go() it will bubble up and stop the JVM.

2

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.

John Watts
  • 8,717
  • 1
  • 31
  • 35