如何在c#中处理异常 [英] How to handle exception in c#

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

问题描述

大家好,



如何正确处理异常?

假设我在例外情况下将异常详细信息记录到文本文件中。但是,如果文件路径不存在/发生其他异常,则在编写异常详细信息时如何管理它。



如果处理异常如何处理在异常记录方法中发生。



谢谢

解决方案

恐慌。



这就是我一般所做的事情。



我实际上做的是工作的原则是如果我犯了严重错误的话不能记录错误 - 所以很可能它不是我的问题 - 这是我无法控制的主要系统故障。所以忽略它。向用户报告,以便他们有机会在任何地方保存任何数据(如果可以),或者杀死您的应用程序。如果你不能记录错误,那么你就不能记录错误的事实(例如,磁盘可能已满),所以你不能做很多事情。如果磁盘已满,那么你甚至不能给自己发一封电子邮件让自己知道,因为这需要电子邮件系统的临时磁盘空间!


阅读我的文章: MVC基本站点:第2步 - 例外管理 [ ^ ]

Hi All,

How to handle exception properly ?
Let say I am logging exception details to a text file on exception.But while writing exception details if file path not exist/some other exception occur how can I manage it.

How to handle exception if it happens in exception logging method.

Thanks

解决方案

Panic.

That''s what I generally do.

What I actually do is work on the principle that something has gone seriously wrong if I can''t log errors - so the chances are that it''s not really my problem - it''s a major system fault that I have no control over. So ignore it. Report it to the user so they have a chance to save any data elsewhere if you can, or kill your application. If you can''t log an error, then you can''t log the fact that there is an error (for example, the disk may be full) so there isn''t a lot you can do. If for example the disk is full, then you can''t even send yourself an email to let yourself know, because that needs temporary disk space at the email system!


Read my article: MVC Basic Site: Step 2 - Exceptions Management[^]


这篇关于如何在c#中处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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