Section 11 Exception and Midi Music

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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值