Exception:
IOException, InterruptedException, RuntimeException(ClassCastException, NullPointerException)
Compiler doesn't check runtime exception, which should be the logic problem in the code.
Methods that might throw a checked exception must announce it with a throws Exception declaration.
If the try or catch has a return statement, finally will still run! Flow jumps to the finally, then back to the return.
When a method throws an exception, that method is popped off the stack immediately, and the exception is thrown to the next method down the stack--the caller.
A try with only a finally (no catch) must still declare the exception.
Midi Music:
1. get a Sequencer and open it.
2. Make a new Sequence (a music CD)
3. Get a new Track from the Sequence (a song)
4. Fill the Track with MidiEvents and give the Sequence to the Sequencer. (noteOn and noteOff)
5. start
public class Music {
public void play(){
try {
Sequencer player = MidiSystem.getSequencer();
player.open();
Sequence seq = new Sequence(Sequence.PPQ,4);
Track track = seq.createTrack();
ShortMessage a =new ShortMessage();
a.setMessage(144,1,44,100);
MidiEvent noteOn = new MidiEvent(a,1);
track.add(noteOn);
ShortMessage b = new ShortMessage();
b.setMessage(128,1,44,100);
MidiEvent noteOff = new MidiEvent(b,16);
track.add(noteOff);
player.setSequence(seq);
player.start();
} catch (MidiUnavailableException e) {
e.printStackTrace();
return;
} catch (InvalidMidiDataException e) {
e.printStackTrace();
} finally {
System.out.println("This is finally even with return");
}
}
public static void main(String[] args) {
Music music = new Music();
music.play();
}
}