cod*_*005 0 java exception throw
以下是"throw use;"显示错误的代码.为什么?如何将throw用于用户定义的异常?举个例子?
class use extends Exception{
public String toString() {
return "too many exceptions";
}
}
class user{
public static void main(String s[]) {
int i=3;
try {
if(i>1)
throw use;
}
catch(use e) {
System.out.println(e.toString());
}
finally{
System.out.println("program executed successfully!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你需要一个异常类的实例来抛出它:
throw new use();
Run Code Online (Sandbox Code Playgroud)
要么
use a = new use();
throw a;
Run Code Online (Sandbox Code Playgroud)
将来请遵循Java命名约定,它将使您的代码更具可读性.(类名应以大写字母开头).