Java 异常链被覆盖

2020-02-18  本文已影响0人  天不错啊

一、前言

问题:正式服务器上有一个BUG,但是本地复现不了。于是查看日志,发现日志居然没有打印有效的错误提示,都是一些代理类的调用。
代码:

try{
   ......
}catch (Exception e) {
  e.printStackTrace();
    if (BusinessException.class == e.getClass()) {
        throw new BusinessException(e.getMessage());
    } else {
        throw new BusinessException("导入失败!");
    }
}

日志:

com.xxx.common.exception.BusinessException: 导入失败!
    at com.xxx.system.service.am.impl.CityMarketServiceImpl.importData(CityMarketServiceImpl.java:964)
    at com.xxx.system.service.am.impl.CityMarketServiceImpl$$FastClassBySpringCGLIB$$5ac06249.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)

结果:抛出的异常被BusinessException覆盖,导致异常链被覆盖,无法写入到日志文件中。

二、解决办法

private static final Logger log = LoggerFactory.getLogger(CityMarketServiceImpl.class);
try{
   ......
}catch (Exception e) {
  e.printStackTrace();
    if (BusinessException.class == e.getClass()) {
        throw new BusinessException(e.getMessage());
    } else {
        log.error(e.getMessage());
        throw new BusinessException("导入失败!");
    }
}

三、总结

日志框架使用不够熟悉。平时代码不够重视日志输出,没有仔细研究过这一块东西。

上一篇下一篇

猜你喜欢

热点阅读