最近我参加了一个面试。技术面试环节提出的问题之一是“如何使用单个语句抛出用户定义的异常”。 我把代码写成
class MyException extends Exception{
public MyExeption(String err){
super(err);
}
class sample{
public static void main(String a[]){
throw new MyException("Error");
}
但他说我使用了 2 个语句,1 个用于 throw 语句,另一个用于 super() 语句。 答案是什么。请清除我的疑问。谢谢
最佳答案
我确定面试官正在找你匿名扩展Exception
:
throw new Exception("Error") {
// Here is what makes the exception user-defined.
// You do not need to override anything, but if you want, you can:
public String getMessage() {
return "Here is your user-defined exception!";
}
};
关于java - 在java中的单个语句中抛出用户定义的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17984470/