.NET和C#异常。什么是合理的抓住 [英] .NET and C# Exceptions. What is it reasonable to catch

查看:112
本文介绍了.NET和C#异常。什么是合理的抓住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明,我来自Java背景。我不做太多C#。两个世界之间有很大的转移,但当然有差异,一方面是异常往往被考虑。

Disclaimer, I'm from a Java background. I don't do much C#. There's a great deal of transfer between the two worlds, but of course there are differences and one is in the way Exceptions tend to be thought about.

我最近回答了一个C#问题表明在某些情况下,这样做是合理的:

I recently answered a C# question suggesting that under some circstances it's reasonable to do this:

 try {
   some work
 } catch (Exeption e) {
       commonExceptionHandler();
 }

(为什么不重要的原因)。我收到一个不太明白的答复:

(The reasons why are immaterial). I got a response that I don't quite understand:


直到.NET 4.0,抓住
异常非常糟糕。这意味着您可以捕获各种
低级别的致命错误,因此伪装
错误。这也意味着在
的某些类型的损坏
触发这样的异常的情况下,任何打开的
最终将在块上执行
,所以即使
callExceptionReporter函数尝试
进行日志退出,甚至可能没有获得
到这一点(最后的块可能
再次抛出,或导致更多的损坏,
或删除从
磁盘或数据库中重要的东西)。

until .NET 4.0, it's very bad to catch Exception. It means you catch various low-level fatal errors and so disguise bugs. It also means that in the event of some kind of corruption that triggers such an exception, any open finally blocks on the stack will be executed, so even if the callExceptionReporter fuunction tries to log and quit, it may not even get to that point (the finally blocks may throw again, or cause more corruption, or delete something important from the disk or database).

我比我意识到更困惑,不同意其中的一些。请其他人评论。

May I'm more confused than I realise, but I don't agree with some of that. Please would other folks comment.


  1. 我知道有很多低级异常我们不想吞下。我的commonExceptionHandler()函数可以合理地推翻这些。这似乎与此答案一致相关问题。哪个说根据你的上下文,使用catch(...)可以接受,提供异常被重新抛出。所以我得出结论,使用catch(异常)并不总是邪恶,默默地吞咽某些例外是。

  1. I understand that there are many low level Exceptions we don't want to swallow. My commonExceptionHandler() function could reasonably rethrow those. This seems consistent with this answer to a related question. Which does say "Depending on your context it can be acceptable to use catch(...), providing the exception is re-thrown." So I conclude using catch (Exception ) is not always evil, silently swallowing certain exceptions is.

短语直到.NET 4它是非常糟糕的Catch异常.NET 4中有什么变化?这是对AggregateException的引用,这可能会给我们一些关于我们捕获的异常的新事物,但是我不认为改变基本的不要吞咽规则。

The phrase "Until .NET 4 it is very bad to Catch Exception" What changes in .NET 4? IS this a reference to AggregateException, which may give us some new things to do with exceptions we catch, but I don't think changes the fundamental "don't swallow" rule.

下一句话真的很麻烦。可以这样做吗?

The next phrase really bothers be. Can this be right?




这也意味着在事件
某些类型的腐败,
触发这样的异常,任何打开的
最终阻止在堆栈将
执行(最后块可能
再次抛出,或导致更多的腐败,
或从
磁盘或数据库中删除重要内容)

It also means that in the event of some kind of corruption that triggers such an exception, any open finally blocks on the stack will be executed (the finally blocks may throw again, or cause more corruption, or delete something important from the disk or database)

我的理解是,如果一些低级代码有

My understanding is that if some low level code had

lowLevelMethod() {
    try {
        lowestLevelMethod();
    } finally {
         some really important stuff
    }
}

在我的代码中我调用lowLevel();

and in my code I call lowLevel();

 try {
      lowLevel()
 } catch (Exception e) {
       exception handling and maybe rethrowing
 }

无论我是否捕获异常,这对终止块的排除无任何影响。当我们离开lowLevelMethod()时,finally已经运行了。如果终于要做任何坏事,比如损坏我的磁盘,那么它会这样做。我抓住异常没有任何区别。如果它到达我的异常块,我需要做正确的事情,但是我不能成为执行dmis执行的原因

Whether or not I catch Exception this has no effect whatever on the excution of the finally block. By the time we leave lowLevelMethod() the finally has already run. If the finally is going to do any of the bad things, such as corrupt my disk, then it will do so. My catching the Exception made no difference. If It reaches my Exception block I need to do the right thing, but I can't be the cause of dmis-executing finallys

推荐答案

p>我的座右铭是处理您可以(或需要)的任何其他异常,并在UnhandledException事件中捕获它们。

My motto is handle what you can (or need to) and let any other exceptions bubble up and catch them in an UnhandledException event.

你是一个正确的在你退出该方法之前,finally块始终被调用(即使在try部分中引发异常)。所以无论你想要捕获out方法的异常还是完全取决于你.....这不应该影响被调用的finally块。

You are correct tho, a finally block is always called (even in the event of an exception being raised in the try section) before you exit the method. So whether you want to the catch the exception on the out method or not is entirely up to you.....this should not affect the finally block being called.

这篇关于.NET和C#异常。什么是合理的抓住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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