Exception研究:
Exception 的构造函数如下:
1) Exception();
2) Exception(String msg);
3) Exception(Throwable cause);
4) Exception(String msg, Throwable cause);
一,构造函数适用场合
当代码块中需要抛异常,而且代码块没有其他异常时用1)和2),否则用3)和4)
二,Exception的处理方法:
法则:不打断程序逻辑的Exception就处理,否则就往上抛
1.继续往上抛
特点:显示所有原始的异常信息,和层次关系
2.try...catch处理后,封装成新Exception往上抛
特点:显示的异常信息既有原始信息,也有自定义信息,封装成的新Exception应为3)或4),否则会造成Exception原始信息丢失。
3.在try..catch处理完毕
特点:需要程序员自己仔细分析,确保异常被正确处理。
三,Exception用i18n
public class BaseException extends Exception {
private static final long serialVersionUID = 1L;
protected String key = null;
@Override
public String getMessage() {
if(key==null){
return "";
}else{
return PropertiesUtil.getProperty(key);
}
}
public BaseException() {
super();
}
public BaseException(String key) {
super(key);
this.key = key;
}
public BaseException(Throwable throwable){
super(throwable);
}
public BaseException(String key,Throwable throwable){
super(key,throwable);
this.key = key;
}
}
Exception研究
最新推荐文章于 2024-08-05 07:45:00 发布