自定义异常(String message,Throwable cause)

时间:2016-02-17 01:02:56

标签: java exception

想问一下使用构造函数

创建新异常对象的正确方法是什么
public AppException(String message, Throwable cause) {
    super(message, cause);
}

如果我想为FileNotFoundException创建一个自定义异常,我可以把它写成

AppException = new AppException("Load fail", FileNotFoundException);

目前上面的语法以

返回给我
error: cannot find symbol 
symbol: variable FileNotFoundException 

1 个答案:

答案 0 :(得分:0)

修复编译错误:

AppException appException = new AppException("Load fail", new FileNotFoundException());

但是!第二个论点应该是例外原因。如果您创建一个新的,则不是这种情况。如果你有原因,你应该使用它。否则创建仅包含message参数的第二个构造函数:

public AppException(String message) {
    super(message);
}

然后这样称呼:

new AppException("Load fail");
相关问题