50分求助,编译器为什么对这一小段练习程序提示这样的错误?
源程序如下:
//目的是想在赋值时若日期过大则抛出异常
//MyDateException 是自定义的异常类
class MyDateException extends Exception{
private String reason;
public MyDateException(String r){
reason=r;
}
public String getReason(){
return (reason);
}
}
class MyDate{
int year,month,day;
void setDate(int y,int m,int d){
if(d>31) {
throw new MyDateException("day too big");//抛出异常
}
year=y;
month=m;
day=d;
}
}
class TestDate{
public static void main(String args[]){
MyDate t=new MyDate();
try{
t.setDate(2003,8,33);
}
catch(MyDateException ee){
System.out.println(ee.getReason());
}
}
}
//编译器提示错误如下;
unreported exception MyDateException; must be caught or declared to be thrown
throw new MyDateException("day too big");