如何从“try catch”中获取异常 [英] How to get the exception from "try catch"

查看:602
本文介绍了如何从“try catch”中获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的项目包含太多try catch,我担心没有完整的异常消息提示,所以我排除错误代码位置非常困难。

我想要获取一个捕获全局异常信息的事件,并且可以从try catch获取完整的异常信息



在我的项目中,有太多的惰性代码



如下:



First of all,My project contains too many "try catch",I worried that there is no complete exception message prompt,So I troubleshoot the error code position is very difficult。
I want to get an event that captures global exception information, and can get the complete exception information from "try catch"

In My project, there are too many lazy code

As follows:

try
{
   //....throw a errr
}
catch{}










try
{
  //....throw two  errr
}
catch(excetion ex)
{
   messagebox.show(ex.message);//but i want to know Stack
}





这样的代码太多了



我需要查看堆栈的详细信息



Such code too much

I need to see detailed information on the stack

推荐答案

要调试应用程序,您应该在配置为以所有异常为起点的调试器下运行它。



如所示在评论中,你应该在没有必要的时候避免本地的try-catch。您很少需要在C#中使用try / catch块。



通常,您可能希望在应用程序范围内处理未处理的异常处理。可以在网上和官方MSDN文档中找到大量信息。顺便说一下,由于你是初学者,首先阅读C#上的一些书可能是有意义的。



http://www.csharp-examples.net/catching-unhandled-exceptions/ [ ^ ]



你们两个示例代码都是通常错了。你很少想像第一个例子那样吞下异常,如果你给用户写了一条消息,你通常会给他一个正确的消息,而不是使用原始消息。



对于某些类型的应用程序,安全性或知识产权很重要,您实际上不希望显示堆栈跟踪,因为它可以提供一些关键信息。
To debug your application, you should run it under a debugger configured to stop on all exception as a starting point.

As indicated in a comment, you should avoid local try-catch when not necessary. You rarely need try/catch blocks in C#.

Usually, you might want to handle unhandled exception handling at application scope. A lot of information can be found on the web and in official MSDN documentation. By the way, since you are a beginner, it might make sense to read some books on C# first.

http://www.csharp-examples.net/catching-unhandled-exceptions/[^]

Both of you sample code are usually wrong. You rarely want to swallow exception as in first example and if you write a message to the user, you will typically give him a proper message and not use the original message.

For some type of application were safety or intellectual property are important, you really don't want to display the stack trace as it can give some crucial information.


https://msdn.microsoft.com/en-us/library/system.exception .stacktrace(v = vs.110).aspx [ ^ ]


您可以在 MessageBox <中向用户显示堆栈跟踪/ code> object。



You can display the stack trace to the user in a MessageBox object.

try
{
  // Code or throw statement
}
catch(Exception ex)
{
  MessageBox.Show(ex.StackTrace);
}


这篇关于如何从“try catch”中获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆