如何获得java中的Exception的堆栈跟踪信息

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tian7wang/article/details/83908725
大家都知道,下面一段代码可以直接输出异常的堆栈跟踪信息:

try

{

...

}

catch(Exception e)

{

e.printStackTrace();

}

可是,事实上在我们开发程序的过程中除非是做调试,一般均采用log4j做为错误信息输出的工具。而Exception本身并未提供将堆栈跟踪信息以字符串形式返回的函数。

/**
*
*/
package net.goldensharp;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
* @author Wesley
*
*/
public class ExceptionUtil
{

/**
* 以字符串形式返回异常堆栈信息
* @param e
* @return 异常堆栈信息字符串
*/
public static String getStackTrace(Exception e)
{
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer,true));

return writer.toString();
}

/**
* @param args
*/
public static void main(String[] args)
{
try
{
throw new Exception("自定义异常");
}
catch(Exception ex)
{
System.out.println(getStackTrace(ex));
}

}

}

文正摘自:[url]http://iwesley.blog.hexun.com/9965836_d.html[/url]
展开阅读全文

哪位大虾知道怎么看“堆栈跟踪信息

02-27

比如:rn[SqlException: 将截断字符串或二进制数据。]rn System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723rn System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) +45rn System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) +5rn System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +304rn System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +77rn System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) +38rn comm.project.getuserrefinfo(Int32 userid) in c:\inetpub\wwwroot\comm\ewk_business\myeworks\project.cs:286rn comm.userrefinfo.BindData() in c:\inetpub\wwwroot\comm\ewk_business\myeworks\userrefinfo.aspx.cs:71rn comm.userrefinfo.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\comm\ewk_business\myeworks\userrefinfo.aspx.cs:43rn System.Web.UI.Control.OnLoad(EventArgs e) +67rn System.Web.UI.Control.LoadRecursive() +35rn System.Web.UI.Page.ProcessRequestMain() +731rn-------------------------------------rn顺序是从下往上看吗?rn后面的+731等数字是什么意思?(43、71那些我想应该是行号吧)rnrn哪位知道?rn多谢 论坛

堆栈跟踪的错误信息

03-01

类似下面的堆栈跟踪信息,如何分析错误的地方。每句话的结构代表什么意思?rnrn[code=HTML]rn[HttpException (0x80004005): 不能在 DropDownList 中选择多个项。]rn System.Web.UI.WebControls.DropDownList.VerifyMultiSelect() +107rn System.Web.UI.WebControls.ListControl.RenderContents(HtmlTextWriter writer) +1828474rn System.Web.UI.WebControls.WebControl.Render(HtmlTextWriter writer) +29rn System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25rn System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121rn System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22rn System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199rn System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20rn System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +59rn System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +68rn System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25rn System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121rn System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +37rn System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +199rn System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +20rn System.Web.UI.Page.Render(HtmlTextWriter writer) +26rn System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +25rn System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +121rn System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +22rn System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2558rnrn[/code] 论坛

没有更多推荐了,返回首页