java异常体系和业务处理

1、java异常体系java

先看Java异常体系图:app

全部异常类的父类为Throwable类,两个直接子类为Error和Exception分别表示错误和异常。ui

一、Error类this

Error是程序没法处理的错误,它是由JVM产生和抛出的,好比OutOfMemoryError、ThreadDeath等。这些异常发生时,Java虚拟机(JVM)通常会选择线程终止。编码

二、Exception类spa

Exception是程序自己能够处理的异常,这种异常分两大类运行时异常(Unchecked Exception)和非运行时异常(Checked Exception)。程序中应当尽量去处理这些异常。线程

RuntimeException(Unchecked Exception):便可以编译经过,通常由程序的逻辑错误引发,开发过程当中应尽可能避免。例如:NullPointerException,IndexOutOfBoundsException等。自定义异常通常继承此类。
3d

RuntimeException之外的异常(checked Exception):编译器在编译阶段进行处理,程序必须处理此类异常不然没法经过编译,如IOException、SQLException等。日志

2、异常的捕获和处理code

异常的捕获经过try、catch、finally三个关键字,三个语句块均不能单独使用,三者能够组成 try...catch...finally、try...catch、try...finally三种结构,catch语句能够有一个或多个,finally语句只能一个

一、普通无异常:

public class TestDemo {
	 public static void main(String[] args) {
	        String result = GetString();
	        System.out.print(result);

	    }
    public static String GetString(){
        try
        {
            System.out.print("GetString():try\n");
            return "GetString()返回值\n";
        }
        catch (Exception e)
        {
            System.out.print("GetString():catch\n");
            return "GetString()返回值";
        }
        finally
        {
            System.out.print("GetString():finally\n");
        }
        //return "这里是方法底部返回值";
    }
}

  运行结果:

整个执行顺序以下:执行顺序是try=>finally=>return

二、出现异常调用状况

public class TestDemo {
	 public static void main(String[] args) {
	        String result = GetString();
	        System.out.print(result);
	    }
    public static String GetString(){
        try
        {
            System.out.print("GetString():try\n");
            int i = 0;
        	int var = 1 / i ;
        	System.out.print("执行GetString():1/i\n");
            return "GetString()try 返回值\n";
        }
        catch (Exception e)
        {
            System.out.print("GetString():catch\n");
            return "GetString()catch 返回值";
        }
        finally
        {
            System.out.print("GetString():finally\n");
        }
        //return "这里是方法底部返回值";
    }
}

  执行结果:

整个执行顺序以下:try=>catch=>finally=>catch return

能够肯定:

1)不论出不出现异常都会执行finally代码块

2)在try模块中出现异常后,异常代码后续流程都不会继续执行

三、try、catch、finally模块中进行赋值

public class TestDemo {
	 public static void main(String[] args) {
	        String result = GetString();
	        System.out.print(result);
	    }
    public static String GetString(){
    	String var = "";
        try
        {
            var = "try var值";
        	System.out.print("执行GetString():var\n");
            return var;
        }
        catch (Exception e)
        {
            System.out.print("GetString():catch\n");
            return "GetString()catch 返回值";
        }
        finally
        {
        	var = "finally var值";
            System.out.print("GetString():finally\n");
        }
    }
}

  执行结果:

经过执行结果能够看到,在finally中对var进行赋值,虽然程序执行了finally模块,可是最终不会影响var的值,var的值仍是在try模块中赋值内容。

结论:虽然finally方法会被执行可是返回结果不会被改变,也就是finally是在return以后执行的,try模块会先把返回结果先保存起来,而后无论finally代码执行了什么,都不会影响到返回结果,等finally执行完成在返回结果。

3、业务如何定义异常

业务中须要按照必定的规范来定义异常,这样有利于系统管理业务异常。

经过上面继承图能够看出,全部service都会继承BaseExcepion,BaseException最终经过RuntimeException来实现。

而在实际的业务中须要准肯定位异常归属的服务和具体类型,这样就能够在对异常日志进行监控时有区分的进行不一样的处理(好比重要的账务或者渠道服务进行较高优先级的提醒或者自处理)。

因此能够在BaseException经过系统定义的code,code经过两个部分来组成:不一样的service定义的值和同一个service中具体异常类型值。

public class BaseException extends RuntimeException {

    private String code;

    public BusinessException() {
        super();
    }

    public BusinessException(String code, String message) {
        super(message);
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * 获取异常编码
     * @param sysCode 系统编号枚举
     * @param exceptionType 异常类型枚举
     * @param exceptionCode 异常顺序码
     * @return String 异常编码
     */
    protected static String getExceptionCode(SysCode sysCode, ExceptionType exceptionType, String exceptionCode) {

        if (StringUtil.isEmpty(exceptionCode) || StringUtil.length(exceptionCode) != 4) {
            throw new BusinessException("00000000", "异常编码不符规范");
        }

        return new StringBuilder(sysCode.getCode()).append(exceptionType.getCode()).append(exceptionCode).toString();
    }

    public BusinessException print(String message) {
        logger.error("BusinessException Code:{};Message:{};OtherMessage:{}", this.code, super.getMessage(), message);
        return this;
    }

  在serviceA中经过调用定义异常类型ORDER_PARAM_NULL,自己ServiceA自带code:SysCode.SER_A,再和定义的1001进行拼接获得最终的该异常在整个系统中的异常code。

public class ServiceAException extends BusinessException {
    public static FeeException ORDER_PARAM_NULL = new ServiceAException(getExceptionCode( "1001"),
            "关键参数为空");    
    public static String getExceptionCode(String exceptionCode) {
        return getExceptionCode(SysCode.SER_A/*这里定义serviceA的系统code值*/,  exceptionCode);
    }
}
相关文章
相关标签/搜索