java基础自定义测试异常类
书上的课后题,我觉得有用就把放这里了
class NoThisSongException extends Exception {
public NoThisSongException() {
super();
}
public NoThisSongException(String str) {
super(str);
}
}
class Player {
public void play(int index) throws NoThisSongException{
if(index>10){
throw new NoThisSongException("您播放的歌曲不存在");
} else {
System.out.println("歌曲"+index+"已经播放");
}
}
}
public class test3 {
public static void main(String[] args) {
Player p1 = new Player();
try{
p1.play(5);
p1.play(11);
}catch(NoThisSongException e){
System.out.println(e.getMessage());
}
}
}