从 Application_UnhandledException 事件中的 StatckTrace 获取方法、类和行号
我正在开发 Windows Phone 7 Silverlight 应用程序。我想做应用程序级别的错误处理,而不是在所有方法中编写 try...catch... 。我需要提取实际发生错误的方法名称、类名称和行号。下面是演示代码。在 Application_UnhandledException 事件中,我期待 Method = "GenerateError" 和 Class = "ExceptionTesting"。另外,我想获取实际发生错误的行号(这未在代码中显示)。
生成错误的代码:
public partial class ExceptionTesting : PhoneApplicationPage
{
// Generate Error to Test Exception Handling
private void GenerateError()
{
Int16 i = Convert.ToInt16("test");
}
}
处理应用程序级别异常的代码:
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
StackTrace st = new StackTrace();
var query = st.GetFrames() // get the frames
.Select(frame => new
{
Method = frame.GetMethod(),
Class = frame.GetMethod().DeclaringType
});
foreach (var q in query)
{
if (q.Method.Name.Contains("GenerateError"))
{
MessageBox.Show("Class: " + q.Class + ", Method: " + q.Method);
}
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
I am developing Windows Phone 7 Silverlight Application. I want to do Application Level error handling instead of writing try...catch... in all methods. I need to extract Method Name, Class Name and Line Number where the actual error occurred. Below is the demo code. In Application_UnhandledException event, I am expecting Method = "GenerateError" and Class = "ExceptionTesting". Also, I want to get LineNumber where the actual error occurred (this is not shown in code).
Code to generate Error:
public partial class ExceptionTesting : PhoneApplicationPage
{
// Generate Error to Test Exception Handling
private void GenerateError()
{
Int16 i = Convert.ToInt16("test");
}
}
Code that Handles Application Level Exception:
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
StackTrace st = new StackTrace();
var query = st.GetFrames() // get the frames
.Select(frame => new
{
Method = frame.GetMethod(),
Class = frame.GetMethod().DeclaringType
});
foreach (var q in query)
{
if (q.Method.Name.Contains("GenerateError"))
{
MessageBox.Show("Class: " + q.Class + ", Method: " + q.Method);
}
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所发现的,
Application_UnhandledException
方法不会从发生异常的方法中调用,因此new StrackTrace()
将没有意义。要获取异常发生位置的堆栈跟踪,请使用
e.Exception.StackTrace
。请注意,真正的异常可能包含在另一个异常中,可能有几层深(
e.Exception.InnerException
)。The
Application_UnhandledException
method is not called from your method where the exception happens, sonew StrackTrace()
will not be meaningful, as you have discovered.To get the stack trace for the place where the exception occurred, use
e.Exception.StackTrace
.Note that the real exception may be wrapped inside another exception, possibly several layers deep (
e.Exception.InnerException
).您还可以使用 BugSense 获取此信息。
免责声明:我是联合创始人之一
You could also use BugSense to get this information.
Disclaimer: I am one of the cofounders