【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 += "\tat " + s + "\r\n";
}
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
【java】Execption的 e.getMessage()为null的解决方法的更多相关文章
- 安装J2EE的SDK报错:could not find the required version of the Java(TM)2 Runtime Environment in '(null)'的解决办法。
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- java.lang.IllegalArgumentException: The observer is null.终于解决方式
java.lang.IllegalArgumentException: The observer is null.终于解决方式 在使用数据适配的时候的问题: java.lang.IllegalArgu ...
- Java高性能编程之CAS与ABA及解决方法
Java高性能编程之CAS与ABA及解决方法 前言 如果喜欢暗色调的界面或者想换换界面,可以看看我在个人博客发布的 Java高性能编程之CAS与ABA及解决方法. CAS概念 CAS,全称Compar ...
- Java常见的几种内存溢出及解决方法
Java常见的几种内存溢出及解决方法[情况一]:java.lang.OutOfMemoryError:Javaheapspace:这种是java堆内存不够,一个原因是真不够(如递归的层数太多等),另一 ...
- java.sql.SQLException: Before start of result set解决方法
java.sql.SQLException: Before start of result set解决方法 今天做东西的时候发现这个错误,查了查,特地记下来,以后开始积累了 哈哈 解决发法是: 使用r ...
- java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource解决方法
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource解决方法 只需把这三个commons-pool.jar ...
- java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法
java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector解决方法 错误描述:java.lang.NoClassDefFoundErro ...
- .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
最近在项目中与别的公司对接业务,对方是Java语言,需要调用对方的WebServices,结果常规的添加web引用的方法可以传过去值,但是返回值为null 查了很多资料,没有解决方法 思考应该是.Ne ...
- 浏览器url传参中文时得到null的解决方法
在写一个中文参数需求的时候遇到了以下问题,经过半天的测试和各种编码,以及网上一些有用没用的资料尝试终于解决 比如下面的url地址:http://travel.widget.baike.com:8 ...
随机推荐
- flink 实现三角枚举EnumTriangles算法详解
1.三角枚举,从所有无向边对中找到相互连接的三角形 /** * @Author: xu.dm * @Date: 2019/7/4 21:31 * @Description: 三角枚举算法 * 三角枚举 ...
- apache配置项
环境:apache2.24 apache 官方文档:http://httpd.apache.org/docs/2.4/ 全部指令索引: http://httpd.apache.org/docs/ ...
- 4-1 Matplotlib 概述
Matplotlib概述 In [1]: import numpy as np import matplotlib.pyplot as plt #pyplot是matplotlib的画图的接口 ...
- Apache:系统找不到指定的文件: No installed ConfigArgs for the service "Apache2"
解决方法: 将以下内容保存成FixApacheError.reg文件(其中红色粗体Apache2改成报错的系统服务名称,如"RTX_HTTPServer"),导入系统注册表 Fix ...
- Delphi-基础(例程、例程返回值)
一.例程:Delphi中独有的称呼,例程是将具体某个功能的代码进行封装表现形式: 1.过程 2.函数 过程和函数的区别在于有没有返回值二.例程的作用 1.可以解决命名冲突问题 2.提高代码的重复使用率 ...
- time,datetime,random,os,sys,hashlib,logging,configparser,re模块
#-----time模块----- print(help(time)) #打印time帮助文档 print(time.time()) #打印时间戳 1569824501.6265268 time.sl ...
- 笔记5:Django知识一
Django 1 MVC MVC的核心思想就是解耦. 2 Django介绍 2.1 MVT Django遵循MVC思想,其称为MVT.其中: M: Model模型:和MVC中的M相同和数据库交互 V: ...
- linux(05) 编译安装py3
一.编译安装python3 https://www.cnblogs.com/pyyu/p/9015317.html 1.下载python3的源码 cd /opt yum install wget -y ...
- Flask框架入门(一)
Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎所有的功能都 ...
- 题解:[ZJOI2014]璀灿光华
原题链接 OJ 题号 洛谷 3342 loj 2203 bzoj 3619 题目描述 金先生有一个女朋友没名字.她勤劳勇敢.智慧善良.金先生很喜欢她.为此,金先生用\(a^3\)块\(1 \times ...