我对异常处理有疑问,可能是愚蠢的问题,但我想知道这个概念。
如何从最内层的方法中获取原始异常。请参阅以下代码。
我期望在第89行的GetData方法中出现异常,在catch块中我记录了异常并返回它直到达到page_load。
On Application_Error
(global.asax)事件,我收到错误:(ex = Server.GetLastError().GetBaseException())
并发送包含堆栈跟踪详细信息的电子邮件。
这里Application_Error stacktrace显示不同的行号(实际错误行89,但它显示97)。为什么呢?
如果发生任何异常,我只想记录并且不想传播到page_load。但是如何在Application_Error事件中获取原始行。? (即。一旦异常发生在任何方法中,我需要记录,我不想去父捕获块等。)
customexception类是否适合?怎么做? (如果我从base(异常类)
Application_error方法(global.asax),我有一个发送电子邮件的代码。
请建议我处理这种情况的最佳解决方案?非常感谢您的帮助。感谢
//输出
ERROR 2015-03-23 19:15:15,249 34008ms GetData() Failed TAPLog - Cannot find table 1.
System.Exception: at System.Data.DataTableCollection.get_Item(Int32 index)
at TAPApplication.Outcome.TestException.GetData(String grid) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 89
ERROR 2015-03-23 19:15:15,322 34080ms LoadGrid() Failed TAPLog - Cannot find table 1.
System.Exception: at System.Data.DataTableCollection.get_Item(Int32 index)
at TAPApplication.Outcome.TestException.GetData(String grid) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 97
at TAPApplication.Outcome.TestException.LoadGrid() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 60
ERROR 2015-03-23 19:15:15,372 34130ms PopulateData() Failed TAPLog - Cannot find table 1.
System.Exception: at System.Data.DataTableCollection.get_Item(Int32 index)
at TAPApplication.Outcome.TestException.GetData(String grid) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 97
at TAPApplication.Outcome.TestException.LoadGrid() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 67
at TAPApplication.Outcome.TestException.PopulateData() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 31
ERROR 2015-03-23 19:15:15,424 34183ms Page_Load() Failed TAPLog - Cannot find table 1.
System.Exception: at System.Data.DataTableCollection.get_Item(Int32 index)
at TAPApplication.Outcome.TestException.GetData(String grid) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 97
at TAPApplication.Outcome.TestException.LoadGrid() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 67
at TAPApplication.Outcome.TestException.PopulateData() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 36
at TAPApplication.Outcome.TestException.Page_Load(Object sender, EventArgs e) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 17
ERROR 2015-03-23 19:15:27,084 45843ms VDPOLogger Application_Error - GetBaseException Stack TraceSystem.IndexOutOfRangeException: Cannot find table 1.
at System.Data.DataTableCollection.get_Item(Int32 index)
at TAPApplication.Outcome.TestException.GetData(String grid) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 97
at TAPApplication.Outcome.TestException.LoadGrid() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 67
at TAPApplication.Outcome.TestException.PopulateData() in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 36
at TAPApplication.Outcome.TestException.Page_Load(Object sender, EventArgs e) in c:\Development\TFS\TAPApplication\Outcome\TestException.aspx.cs:line 22
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
示例代码:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TAPApplication.Outcome
{
public partial class TestException : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
PopulateData();
}
catch(Exception ex)
{
EventLogger.TAPLog("Page_Load() Failed", ex.Message, ex.StackTrace, Utility.ErrorTypes.Error); //line 21
throw;
}
}//line 24
public void PopulateData()
{
try
{
LoadChart();
LoadGrid();
}
catch(Exception ex)
{
EventLogger.TAPLog("PopulateData() Failed", ex.Message, ex.StackTrace, Utility.ErrorTypes.Error); //line 35
throw;
}
}//line 38
public void LoadChart()
{
try
{
//
//
//
//
}
catch(Exception ex)
{
EventLogger.TAPLog("LoadChart() Failed", ex.Message, ex.StackTrace, Utility.ErrorTypes.Error); //line 51
throw; //line 52
}//line 53
}//line 54
public void LoadGrid()
{
try
{
GridView1.DataSource = GetData("Grid");
GridView1.DataBind();
}
catch (Exception ex)
{
EventLogger.TAPLog("LoadGrid() Failed", ex.Message, ex.StackTrace, Utility.ErrorTypes.Error); //line 66
throw; //line 67
} //line 68
}//line 69
public DataSet GetData(string grid)
{
DataSet ds = new DataSet();
try
{
DataTable dt = new DataTable("Table1");
dt.Columns.Add("Month");
dt.Columns.Add("IceCream");
dt.Columns.Add("Chocolate");
dt.Columns.Add(new DataColumn("Explode") { DefaultValue = false });
dt.Rows.Add(DateTime.Now, 25, 67);
dt.Rows.Add(DateTime.Now.AddDays(1), 33, 63);
dt.Rows.Add(DateTime.Now.AddDays(2), 34, 64);
dt.Rows.Add(DateTime.Now.AddDays(3), 63, 43);
dt.Rows.Add(DateTime.Now.AddDays(4), 74, 45);
dt.Rows.Add(DateTime.Now.AddDays(5), 0.48, 74);
ds.Tables.Add(dt);
if (ds.Tables[1].Rows.Count>0) //line 89
{
//
}
}
catch(Exception ex)
{
EventLogger.TAPLog("GetData() Failed", ex.Message, ex.StackTrace, Utility.ErrorTypes.Error); //line 96
throw; //line 97
}
return ds;
}
}
}
的web.config
<customErrors mode="RemoteOnly" defaultRedirect="Errors/DefaultError.aspx">
<error statusCode="400" redirect="Errors/Error400.aspx"/>
<error statusCode="401" redirect="Errors/Error401.aspx"/>
<error statusCode="404" redirect="Errors/Error404.aspx"/>
<error statusCode="500" redirect="Errors/Error500.aspx"/>