1 自定义异常
2 实现代码
class MyException extends Exception{
private String message;
public MyException(String message){
this.message = message;
}
public void getmessage(){
System.out.println(message);
}
}
class Display{
public static void show(Integer data) throws MyException{
if (data < 0){
throw new MyException(data.toString() + " less zero");
}
System.out.println(data);
}
}
public class Test{
public static void main(String[] args){
try {
Display.show(-2);
}catch (MyException e){
e.getmessage();
}
}
}