在C#全球的异常处理(控制台应用程序) [英] Global exception handling in c# (console application)

查看:239
本文介绍了在C#全球的异常处理(控制台应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果

我读过很多关于计算器全球异常处理的事情,但我无法找到任何东西指的是我演讲的题目正常。我写了异常捕获刚刚在它们发生的方法的控制台应用程序。不扔的都没有。我想已经是一个全球性的异常处理。我没有对异常处理的基本指导,推进但有不知道超出基本知识。想象应该有一个类中处理这些异常(来自不同阶层的方法抛出)。我不需要任何错误记录或错误报告机制还没有。刚需的基本知识(图案什么...)来实现这一全球性的处理。感谢您的阅读!

I've read a lot of things about global exception handling on stackoverflow, but I could not find anything refering to my topic properly. I wrote a console application that catches exceptions just in the methods they occur. No throw's at all. What I want to have is a global exception handling. I no the basic guidlines about exception handling, but got no idea beyond those basics. Imaginary there should be one class handling those exceptions (thrown by the methods from the different classes). I don't need any error-recording or error-reporting mechanisms yet. Just need the basics (patterns whatever...) to implement this global handling. Thanks for reading so far!

推荐答案

在全球范围处理异常是只适合某些任务,如记录错误或曼妙的出口应用程序。

Handling exceptions globally is good for certain tasks only, such as logging errors or graceful exit of the application.

在所有其他情况下,建议您处理异常最相关的地方。例如,如果您从您的应用程序使Web服务调用,如果服务不可用,你应该处理异常的地方,你让Web服务调用,而不是在全球捕获它。这提高了代码的可读性,而且它不是实际的,你有异常来自何处的信息很少做任何事明智在全球层面。 (当然,你必须堆栈​​跟踪,但它不是现成的程序操作)

In all other cases, it is recommended that you handle the exception in the most relevant place. For example, if you make a web service call from your app and if the service is not available, you should handle that exception where you make the webservice call instead of catching it globally. This improves the readability of the code, and also it's not practical to do anything sensible at global level as you have very little information on where the exception originated. (Of course you have the stack trace, but its not readily available for programmatic manipulation)

不过,您可以通过附加一个处理程序<一个添加应用程序广泛异常处理器HREF =htt​​p://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx相对=nofollow> AppDomain.UnhandledException 作为也在发表谈话表示@Jesse

However, you can add an application wide exception handler by attaching a handler to AppDomain.UnhandledException as also indicated in the comment by @Jesse

这篇关于在C#全球的异常处理(控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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