C# Catch vs Catch (Exception e) 和 Throw vs Throw e
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10805987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Catch vs Catch (Exception e) and Throw vs Throw e
提问by a1204773
Are these two code examples the same? Catchand Catch (Exception e)have the same output, and the result is also the same if I write Throwor Throw e.
这两个代码示例相同吗?Catch和Catch (Exception e)具有相同的输出,如果我写Throw或Throw e,结果也相同。
Main:
主要的:
try
{
A();
//B();
}
catch (Exception e)
{
Console.WriteLine("{0} exception caught.", e);
}
Code 1:
代码 1:
static void A()
{
try
{
int value = 1 / int.Parse("0");
}
catch (Exception e)
{
throw e;
}
}
Code 2:
代码 2:
static void A()
{
// Rethrow syntax.
try
{
int value = 1 / int.Parse("0");
}
catch
{
throw;
}
}
采纳答案by Mark Byers
I think there are two questions here.
我认为这里有两个问题。
What is the difference between throwand throw e;?
throw和 和有throw e;什么区别?
I don't think there is ever a good reason to write catch (Exception e) { throw e; }. This loses the original stacktrace. When you use throw;the original stacktrace is preserved. This is good because it means that the cause of the error is easier to find.
我不认为有充分的理由去写catch (Exception e) { throw e; }。这会丢失原始堆栈跟踪。使用时throw;保留原始堆栈跟踪。这很好,因为这意味着更容易找到错误的原因。
What is the difference between catchand catch (Exception e)?
catch和 和有catch (Exception e)什么区别?
Both of your examples are the same and equally useless - they just catch an exception and then rethrow it. One minor difference is that the first example will generate a compiler warning.
你的两个例子都是一样的,同样没用——它们只是捕获一个异常然后重新抛出它。一个细微的区别是第一个示例将生成编译器警告。
The variable 'e' is declared but never used
变量“e”已声明但从未使用
It makes more sense to ask this question if you had some other code in your catch block that actually does something useful. For example you might want to log the exception:
如果您的 catch 块中有一些其他代码实际上做了一些有用的事情,那么问这个问题会更有意义。例如,您可能想要记录异常:
try
{
int value = 1 / int.Parse("0");
}
catch (Exception e)
{
LogException(e);
throw;
}
Now it's necessary to use the first version so that you have a reference to the caught exception.
现在有必要使用第一个版本,以便您可以引用捕获的异常。
If your catch block doesn't actually use the exception then you would want to use the second version to avoid the compiler warning.
如果您的 catch 块实际上没有使用异常,那么您将希望使用第二个版本来避免编译器警告。
回答by Marc Gravell
If we ignore the "unused variable" warning, the only time there is a practicaldifference between
如果我们忽略“未使用的变量”警告,唯一的一次是两者之间存在实际差异
catch {...}
and
和
catch(Exception ex) {...}
is when some non-C# code is throwing a non-Exceptionexception. C++ can throw anything. In .NET 1.1, you hadto use catch(no (Exception ex)) to handle these unusual exceptions. However, this was problematic - not least, you can't see what was thrown! So in .NET 2.0 and above this is wrapped by default, so even if C++ throws, say, a string- you see it as an Exceptionsubclass. Note that this can be disabled via a configuration setting, but: don't. Leave it alone!
是当某些非 C# 代码抛出非Exception异常时。C++ 可以抛出任何东西。在 .NET 1.1 中,您必须使用catch(no (Exception ex)) 来处理这些不寻常的异常。然而,这是有问题的——尤其是,你看不到抛出了什么!因此,在 .NET 2.0 及更高版本中,默认情况下这是包装的,因此即使 C++ 抛出 a string- 您也将其视为Exception子类。请注意,这可以通过配置设置禁用,但是:不要。不要管它!
The issue of throw;vs throw ex;is already mentioned, and relates to stack-traces. You can use throwin both cases, causing the original stack-trace to be preserved.
已经提到了throw;vs的问题throw ex;,并且与堆栈跟踪有关。您可以throw在这两种情况下使用,从而保留原始堆栈跟踪。

