美文网首页
Java 异常链被覆盖

Java 异常链被覆盖

作者: 天不错啊 | 来源:发表于2020-02-18 11:33 被阅读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("导入失败!");
        }
    }
    

    三、总结

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

    相关文章

      网友评论

          本文标题:Java 异常链被覆盖

          本文链接:https://www.haomeiwen.com/subject/hlfgfhtx.html