class ChaozaiException extends Exception{
int id;
ChaozaiException(String message,int id){
super(message);
this.id=id;
}
public int getId(){
return id;
}
}
public class PeopleToTravel{
static int num;
public int gotoPark(int num)throws ChaozaiException{
this.num=num;
if(num>100){
throw new ChaozaiException("人数过多,已超载!",1234);
}
return num;
}
public static void main(String[] args){
PeopleToTravel p = new PeopleToTravel();
try{
p.gotoPark(102);
}catch(ChaozaiException ce){
System.out.println(ce.getMessage()+"错误编号:"+ce.getId());
num=100;
}
System.out.println("实际去的人数为:"+num);
}
}
输出结果:人数过多,已超载!错误编号:1234
实际去的人数为:100