这样做更好吗:

try
{
    ...
}
catch (Exception ex)
{
    ...
    throw;
}

或:

try
{
    ...
}
catch (Exception ex)
{
    ...
    throw ex;
}

他们做同样的事情吗?一个比另一个好吗?


当前回答

我的偏好是使用

try
{
}
catch (Exception ex)
{
     ...
     throw new Exception ("Add more context here", ex)
}

这保留了原来的错误,但是它允许您添加更多的上下文,例如对象ID、连接字符串等等。我的异常报告工具通常有五个链接的异常要报告,每个报告更详细。

其他回答

如果抛出一个没有变量的异常(第一个例子),堆栈跟踪将包括抛出异常的原始方法。

在第二个示例中,堆栈跟踪将被更改以反映当前方法。

例子:

static string ReadAFile(string fileName) {
    string result = string.Empty;
    try {
        result = File.ReadAllLines(fileName);
    } catch(Exception ex) {
        throw;    // This will show ReadAllLines in the stack trace
        throw ex; // This will show ReadAFile in the stack trace
    }

我发现,如果在捕获异常的同一方法中抛出异常,堆栈跟踪就不会被保留。

void testExceptionHandling()
{
    try
    {
        throw new ArithmeticException("illegal expression");
    }
    catch (Exception ex)
    {
        throw;
    }
    finally
    {
        System.Diagnostics.Debug.WriteLine("finally called.");
    }
}

我的偏好是使用

try
{
}
catch (Exception ex)
{
     ...
     throw new Exception ("Add more context here", ex)
}

这保留了原来的错误,但是它允许您添加更多的上下文,例如对象ID、连接字符串等等。我的异常报告工具通常有五个链接的异常要报告,每个报告更详细。

你应该总是使用"throw;"来重新抛出。net中的异常,

请参考博客文章《甩和甩前男友》。

基本上,MSIL (CIL)有两个指令——“throw”和“rethrow”,c#的“throw ex;”被编译成MSIL的“throw”和c#的“throw;”-进入MSIL“重扔”!基本上,我可以看到为什么“throw ex”覆盖堆栈跟踪的原因。

第一个比较好。如果尝试调试第二个异常并查看调用堆栈,则无法看到原始异常来自何处。如果真的需要重新抛出,有一些技巧可以保持调用堆栈的完整性(尝试搜索,它之前已经被回答过了)。