异常总体上分为2类:
- checked exceptions:
可被Java Compiler 检查的异常,可认为是除 RuntimeException类型 之外的其他异常 - unchecked exceptions:
可通过编译,在程序运行时抛出的异常
在项目设计中,我们常常自定义系统的业务异常,当业务上不满足校验时,可以通过抛出业务异常,到 catch 语句中做统一的处理,例如打印日志,返回调用方错误信息等。举例如下:
public class SettleException extends RuntimeException {
private static final long serialVersionUID = -694489771576921331L;
private String code;
public SettleException(String code, String msg) {
super(msg);
this.code = code;
}
public SettleException(RetCode ret) {
super(ret.getDescription());
this.code = ret.getCode();
}
public SettleException(RetCode retMsg, String msg) {
super(msg);
this.code = retMsg.getCode();
}
public SettleException(RetCode retMsg, String msg