java:获取异常的堆栈信息

做的java开发的时候,经常会遇到代码抛异常后,需要把异常信息保存到数据库或者上传到云服务器做缓存分析。这时候就需要获取异常的堆栈信息(详细错误信息)。

人有的用e.getMessage()来电子杂志异常信息,但是这样获取到的信息内容并不全,而且有时候为空。我们可以用下面方法来获取。

   public static String getStackTrace(Throwable throwable)
    {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        try
        {
            throwable.printStackTrace(pw);
            return sw.toString();
        } finally
        {
            pw.close();
        }
    }

使用也很简单:

  public static void test()
    {
        try
        {
            int i = 0;
            int m = 10 / i;
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
            System.out.println("------调皮的分割线------");
            System.out.println(ExceptionUtil.getStackTrace(e));
        }
    }

也可以放在request中:

  request.setAttribute(“errorCode1”,getStackTrace(e));
  logger.error(e.getMessage(),e);

我们看下打印结果: 

这里写图片描述

以下是项目中配置的相关信息:

1、在web.xml中添加拦截器:在执行方法前后拦截,如果出现异常捕获并放入request中

  <filter>
    <filter-name>initContextFilter</filter-name>
    <filter-class>
      com.hys.framework.web.filter.InitContextFilter
    </filter-class>
    <init-param>
      <param-name>RequestEncoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>ResponseEncoding</param-name>
      <param-value>utf-8</param-value>
    </init-param>
    <init-param>
      <param-name>FailureForwardName</param-name>
      <param-value>/commons/failure.jsp</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>initContextFilter</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>

2、拦截器类(稍后添加)


3、异常错误页面:fail.jsp

<%@	page contentType="text/html;charset=utf-8"%>
<!DOCTYPE HTML>
<html>
	<head>
	<%@ include file="/commons/taglibs.jsp"%>
		<title>Failure</title>
		<link href="${ctx}/css/style1.css" type=text/css rel=stylesheet>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<script type="text/javascript">
			function forback()
			{
				if(window.history.length==0)
				{
					window.close();
				}
				else
				{
					window.history.back();
				}
			}
	</script>
		<style type="text/css">
#Error {
	COLOR: #B22222;
	FONT-SIZE: 14px;
}
</style>
	</head>
	<body leftmargin="0" topmargin="0" class="body1">
		<center>
			<TABLE width="100%" height="600" border="0" cellPadding="0"
				cellSpacing="0">
				<tr>
					<td align="center">
						<table width="70%" border="0" cellpadding="1" cellspacing="0"
							bordercolor="#BDC7D6" bgcolor="#BDC7D6">
							<tr>
								<td>
									<table width="100%" border="0" cellpadding="0" cellspacing="0"
										bordercolor="#BDC7D6" bgcolor="#FFFFFF" class="lankuang1">
										<tr>
											<td height="25" align="left" valign="middle"
												bgcolor="#EFF3F7" class="lanbottom">
												<strong style="color: #5F9EA0">报错提醒:${ErrorMsg }</strong>
											</td>
										</tr>
										<tr>
											<td height="120" align="center">
												<table width="90%" border="0" cellspacing="0"
													cellpadding="0">
													<tr>
														<td width="73%" align="left" valign="middle">
															<span onclick="showError()" style="cursor:pointer">出错了,请联系管理员!</span>
														</td>
													</tr>
													<tr>
														<td colspan="2" align="center">
															<input type="button" name="forward" value="返回  "
																class="but2" onClick="forback();">
														</td>
													</tr>
												</table>
											</td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
			</TABLE>
						</center>
													<div style="display:none" id="errorcode">
														<span id="Error">信息:${msg }</span>
														<br/>
														---------------------------------
														堆栈:
														${stack }
														-----------
													</div>
	
	</body>
	<script type="text/javascript">
		function showError(){
			document.getElementById("errorcode").style.display="block";
		}
	</script>
</html>

最后效果:


猜你喜欢

转载自blog.csdn.net/nature_fly088/article/details/80640576