一、java异常体系
先看Java异常体系图:
所有异常类的父类为Throwable类,两个直接子类为Error和Exception分别表示错误和异常。
1、Error类
Error是程序无法处理的错误,它是由JVM产生和抛出的,比如OutOfMemoryError、ThreadDeath等。这些异常发生时,Java虚拟机(JVM)一般会选择线程终止。
2、Exception类
Exception是程序本身可以处理的异常,这种异常分两大类运行时异常(Unchecked Exception)和非运行时异常(Checked Exception)。程序中应当尽可能去处理这些异常。
RuntimeException(Unchecked Exception):即可以编译通过,一般由程序的逻辑错误引起,开发过程中应尽量避免。例如:NullPointerException,IndexOutOfBoundsException等。自定义异常一般继承此类。
RuntimeException以外的异常(checked Exception):编译器在编译阶段进行处理,程序必须处理此类异常否则无法通过编译,如IOException、SQLException等。
二、异常的捕获和处理
异常的捕获通过try、catch、finally三个关键字,三个语句块均不能单独使用,三者可以组成 try...catch...finally、try...catch、try...finally三种结构,catch语句可以有一个或多个,finally语句只能一个
1、普通无异常:
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
2、出现异常调用情况
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模块中出现异常后,异常代码后续流程都不会继续执行
3、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执行完成在返回结果。
三、业务如何定义异常
业务中需要按照一定的规范来定义异常,这样有利于系统管理业务异常。
通过上面继承图可以看出,所有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);
}
}
|