java自定义异常
简单记录一下
//输入年龄,超出0~100则抛出异常
class Customer{
private int age;
public void setAge(int age)throws ageexception {
if(0<age&&age<100) {
this.age = age;
}
else {
ageexception e = new ageexception("error");
throw e;
}
}
}
class ageexception extends Exception{
public ageexception(String message) {
super(message);
}
}
class ToSuffixExpression {
public static void main(String[] args) {
String s = javax.swing.JOptionPane.showInputDialog("输入年龄");
int a = Integer.parseInt(s);
Customer c = new Customer();
try {
c.setAge(a);
}catch(ageexception e) {
System.out.println(e.getMessage());
}
}
}