您的位置:首页 > 编程语言 > Java开发

Java自定义异常(优雅的处理异常)

2018-03-16 15:38 555 查看
(本文较长,精华部分直接下拉)
在复杂业务环境下,java自带的异常可能满足不了我们业务的需求, 这个时候我们可以自定义异常来进行对业务异常的处理;
首先,我们先对异常进行基本的解释:
Throwable是所有Java程序中错误处理的父类 ,有两种子类:Error和Exception。

Throwable是所有异常的根,java.lang.Throwable 
Error是错误,java.lang.Error 
Exception是异常,java.lang.ExceptionError:表示由JVM所侦测到的无法预期的错误,由于这是属于JVM层次的严重错误 ,导致JVM无法继续执行,因此,这是不可捕捉到的,无法采取任何恢复的操作,顶多只能显示错误信息。 Error类体系描述了Java运行系统中的内部错误以及资源耗尽的情形.应用程序不应该抛出这种类型的对象(一般是由虚拟机抛出).假如出现这种错误,除了尽力使程序安全退出外,在其他方面是无能为力的。Exception:表示可恢复的例外,这是可捕捉到的.分为运行时异常,检查性异常.
Java提供了两类主要的异常 :runtime exception和checked exception。**checked异常:I**O异常,以及SQL异常都是这种异常。 对于这种异常, JAVA编译器强制要求我们必需对出现的这些异常进行catch 。所以,面对这种异常不管我们是否愿意,只能自己去写一大堆catch块去处理可能的异常。 这类异常一般是外部错误,例如试图从文件尾后读取数据等,这并不是程序本身的错误,而是在应用环境中出现的外部错误。 
runtime exception 运行时异常:我们可以不处理。当出现这样的异常时,总是由虚拟机接管。出现运行时异常后,系统会把异常一直往上层抛,一直遇到处理代码。如果没有处理块,到最上层,如果是多线程就由Thread.run()抛出,如果是单线程就被main()抛出。抛出之后,如果是线程,这个线程也就退出了。如果是主程序抛出的异常,那么这整个程序也就退出了。运行时异常是Exception的子类,也有一般异常的特点,是可以被Catch块处理的。只不过往往我们不对他处理罢了。也就是说,你如果不对运行时异常进行处理,那么出现运行时异常之后,要么是线程中止,要么是主程序终止。 如果不想终止,则必须扑捉所有的运行时异常,决不让这个处理线程退出。队列里面出现异常数据了,正常的处理应该是把异常数据舍弃,然后记录日志。不应该由于异常数据而影响下面对正常数据的处理。在这个场景这样处理可能是一个比较好的应用,但并不代表在所有的场景你都应该如此。如果在其它场景,遇到了一些错误,如果退出程序比较好,这时你就可以不太理会运行时异常,或者是通过对异常的处理显式的控制程序退出。
废话不多说,上干货
自定义,运行时异常;
先定义错误码枚举;规范错误集合public enum OrderExceptionEnum {

/** 未知异常 */
UNKNOWN_EXCEPTION("OE001","未知异常","warn"),
/** 系统错误 */
SYSTEM_ERROR("OE002","系统错误","error")
;

private String errorCode;
private String errorMsg;
private String errorType;

OrderExceptionEnum(String errorCode, String errorMsg, String errorType) {
this.errorCode = errorCode;
this.errorMsg = errorMsg;
this.errorType = errorType;
}

/**
* Getter method for property <tt>errorCode</tt>.
*
* @return property value of errorCode
*/
public String getErrorCode() {
return errorCode;
}

/**
* Setter method for property <tt>errorCode</tt>.
*
* @param errorCode value to be assigned to property errorCode
*/
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

/**
* Getter method for property <tt>errorMsg</tt>.
*
* @return property value of errorMsg
*/
public String getErrorMsg() {
return errorMsg;
}

/**
* Setter method for property <tt>errorMsg</tt>.
*
* @param errorMsg value to be assigned to property errorMsg
*/
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}

/**
* Getter method for property <tt>errorType</tt>.
*
* @return property value of errorType
*/
public String getErrorType() {
return errorType;
}

/**
* Setter method for property <tt>errorType</tt>.
*
* @param errorType value to be assigned to property errorType
*/
public void setErrorType(String errorType) {
this.errorType = errorType;
}
}再定义自定义异常类
public class OrderPeriodException extends RuntimeException {

private static final long serialVersionUID = 6958499248468627021L;
/** 错误码 */
private String errorCode;
/** 错误上下文 */
private ErrorContext errorContext;

public OrderPeriodException(String errorCode, String errorMsg){
super(errorMsg);
this.errorCode = errorCode;
}
public OrderPeriodException(OrderExceptionEnum orderExceptionEnum){
super(orderExceptionEnum.getErrorMsg());
this.errorCode = orderExceptionEnum.getErrorCode();
}

public OrderPeriodException(String errorCode, String errorMsg,Throwable throwable){
super(errorMsg,throwable);
this.errorCode = errorCode;
}
public OrderPeriodException(OrderExceptionEnum orderExceptionEnum,Throwable throwable){
super(orderExceptionEnum.getErrorMsg(),throwable);
this.errorCode = orderExceptionEnum.getErrorCode();
}
/**
* Getter method for property <tt>errorCode</tt>.
*
* @return property value of errorCode
*/
public String getErrorCode() {
return errorCode;
}

/**
* Setter method for property <tt>errorCode</tt>.
*
* @param errorCode value to be assigned to property errorCode
*/
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}

/**
* Getter method for property <tt>errorContext</tt>.
*
* @return property value of errorContext
*/
public ErrorContext getErrorContext() {
return errorContext;
}

/**
* Setter method for property <tt>errorContext</tt>.
*
* @param errorContext value to be assigned to property errorContext
*/
public void setErrorContext(ErrorContext errorContext) {
this.errorContext = errorContext;
}

public static void main(String[] args) {
try{
int i = 1/0;
}catch (Exception e){
throw new OrderPeriodException(OrderExceptionEnum.SYSTEM_ERROR,e);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐