java 自定义异常处理示例
class NoThisSongException extends Exception{
public NoThisSongException(String string){
super(string);
}
NoThisSongException(){
super();
}
}
class Player{
void play(int index) {
try {
if(index<10)
System.out.println("输入正常");
else
throw new NoThisSongException("您播放的歌曲不存在");
}catch(NoThisSongException e) {
//e.getMessage();
System.out.print(e);
}
}
}
public class Test2 {
public static void main(String[] args) {
Player player = new Player();
player.play(11);
}
}