版权声明:本文为博主原创文章,如需转载请标明出处。 https://blog.csdn.net/DGH2430284817/article/details/87278096
java编程中,自带有很多的异常,但是有时候我们也需要自定义一些异常,方便我们阅读日志时更快的找到问题所在位置,所以一个项目或者一个模块都会有自己的异常类,记录程序中可能出现的异常。
异常类:比较杂,大家可以根据不同的需要自定义自己的异常类型
/**
* @author 异常类型
*
* @param VierERR1000 页面异常
* @param MSG1000 "请求参数为空"
* @param MSG2000"参数异常"
* @param UnknowERR9999 未知错误
* @param MSG9999 系统未知错误
* @param SysERR9000 系统异常
* @param DaoERR2000 数据库异常
* @param ServERR3000 服务器(业务逻辑)异常
* @param ActionERR3000 控制器异常
* @param UtilERR3000工具类异常
* @param MSG3000服务器异常
* @param MsgERR3000数据异常
*/
public class ZhfdcException extends Exception{
private static final long serialVersionUID = 1L;
//定义自己的异常类型(以后只需要改这下面的一段代码和工具类名称,其他地方不需要修改)
public static final String VierERR1000 = "VierERR1000";//页面异常
public static final String MSG1000 = "请求参数为空";
public static final String MSG2000 = "参数异常";
public static final String UnknowERR9999 = "UnknowERR9999";//未知错误
public static final String MSG9999 = "系统未知错误";
public static final String SysERR9000 = "SysERR9000";//系统异常
public static final String DaoERR2000 = "DaoERR2000";//数据库异常
public static final String ServERR3000 = "ServERR3000";//服务器(业务逻辑)异常
public static final String ActionERR3000 = "ActionERR3000";//控制器异常
public static final String UtilERR3000 = "UtilERR3000";//工具类异常
public static final String MsgERR3000 = "MsgERR3000";//数据异常
public static final String MSG3000 = "服务器异常";
private String code = SysERR9000;
private String msg = MSG9999;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public ZhfdcException() {
this(SysERR9000,MSG9999, null);
}
public ZhfdcException(String code, String msg) {
this(code, msg, null);
}
public ZhfdcException(String code, Throwable e) {
this(code, e.getMessage(), e);
}
public ZhfdcException(String code, String msg, Throwable e) {
super(code+":"+msg, e);
this.code = code;
this.msg = msg;
}
public ZhfdcException(Exception e) {
this(SysERR9000, e.getMessage(), e);
}
}
测试:
自定义的一些检查异常:如检查非空,检查状态,检查大小之类的
public class testA {
public static void main(String[] args) {
String msg = null;// 请求数据
try {//自定义的检查异常
if (msg == null) {
throw new pojoException(pojoException.MSG1000, "请求数据检查为空");
}
} catch (pojoException e) {
System.out.println("操作异常:" + e.getMessage());
e.printStackTrace();
}
}
}
结果:
操作异常:请求参数为空:请求数据检查为空
exception.pojoException: 请求参数为空:请求数据检查为空
at exception.testA.main(testA.java:11)
因代码错误导致的系统异捕获:
public static void main(String[] args) {
String msg = null;// 请求数据
try {
try {
int mub = Integer.valueOf(msg);
System.out.println("数据转换成功");
} catch (Exception e) {
System.out.println("数据转换异常:" + e.getMessage());
throw new pojoException(pojoException.MSG2000, e.getMessage(), e);
}
} catch (pojoException e) {
System.out.println("操作异常:" + e.getMessage());
e.printStackTrace();
}
}
结果:
数据转换异常:null
操作异常:参数异常:null
exception.pojoException: 参数异常:null
at exception.testA.main(testA.java:22)
Caused by: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:542)
at java.lang.Integer.valueOf(Integer.java:766)
at exception.testA.main(testA.java:19)