【发布时间】:2019-04-19 07:28:42
【问题描述】:
我想通过添加创建用户定义的异常,但出现编译错误。
class Test{
public static void main(String[] args){
int a=9, b=67, result;
result = a+b;
throw new Add("Addition has been performed");
}
}
class Add extends Throwable{
Add(){
printStackTrace();
}
Add(String message){
System.out.println(message);
printStackTrace();
}
}
错误是:
Practice.java:8: error: unreported exception Add; must be caught or declared to be thrown
throw new Add("Addition has been performed");
【问题讨论】:
-
这两个类在同一个文件中?
-
了解已检查和未检查的异常。
-
是的,它们在同一个文件中。