• 【java】Execption的 e.getMessage()为null的解决方法


    ================================

    场景:

      当代码出现异常时通常都需要将异常信息写入到日志中,异常信息越详细越有利于问题的排查。而通过的Exception.getMessage()方法只能获得异常的名称而不能获取哪里出现的异常,对于排错意义不大。

    甚至有时候,getMessage()返回的是null。

    查看getMessage()的源码:

    /**
         * Returns the detail message string of this throwable.
         *
         * @return  the detail message string of this {@code Throwable} instance
         *          (which may be {@code null}).
         */
        public String getMessage() {
            return detailMessage;
        }

    可以看到说明,与可能返回为null。

    解决方法:

    罗列四个解决方法

    //1、
    public String getTrace(Throwable t) {
        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        return buffer.toString();
    }
     
    //2、
    public static String getExceptionAllinformation(Exception ex){
            String sOut = "";
            StackTraceElement[] trace = ex.getStackTrace();
            for (StackTraceElement s : trace) {
                sOut += "	at " + s + "
    ";
            }
            return sOut;
    }
     
    //3、
    public static String getExceptionAllinformation_01(Exception ex) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            PrintStream pout = new PrintStream(out);
            ex.printStackTrace(pout);
            String ret = new String(out.toByteArray());
            pout.close();
            try {
                 out.close();
            } catch (Exception e) {
            }
            return ret;
    }
     
    //4、
    private static String toString_02(Throwable e){  
             StringWriter sw = new StringWriter();  
             PrintWriter pw = new PrintWriter(sw, true);  
             e.printStackTrace(pw);  
             pw.flush();   
             sw.flush();  
             return sw.toString(); 
    } 

    具体使用场景:

              try {
                       .....
    
                    }catch (Exception e){
                        StringWriter stringWriter= new StringWriter();
                        PrintWriter writer= new PrintWriter(stringWriter);
                        e.printStackTrace(writer);
                        StringBuffer buffer= stringWriter.getBuffer();
                        String errMsg = buffer.toString();
    
                        logger.error(errMsg);
    
                        hashMap.put("status",String.valueOf(-1));
                        hashMap.put("errorMsg",StringUtils.isBlank(errMsg) ? "" : errMsg);
                    }finally {
                        redisUtil.HASH.hmset(recordKey,hashMap);
                        String lpop = redisUtil.LISTS.lpop(runTaskKey);
                        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>任务执行完成,执行队列"+runTaskKey+"移除:"+lpop);
                        hashMap.clear();
                    }

    参考地址:

    https://blog.csdn.net/wjiaoling136/article/details/84903619

  • 相关阅读:
    今日SGU 5.27
    今日SGU 5.26
    今日SGU 5.25
    软件工程总结作业
    个人作业——软件产品案例分析
    个人技术博客(α)
    结对作业二
    软工实践 二
    软工实践 一
    《面向对象程序设计》六 GUI
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/11635142.html
Copyright © 2020-2023  润新知