有一些帖子问这两者之间已经有什么区别了。(为什么我要提这个…)

但我的问题在某种程度上是不同的,我在另一种错误处理方法中调用了“throw ex”。

public class Program {
    public static void Main(string[] args) {
        try {
            // something
        } catch (Exception ex) {
            HandleException(ex);
        }
    }

    private static void HandleException(Exception ex) {
        if (ex is ThreadAbortException) {
            // ignore then,
            return;
        }
        if (ex is ArgumentOutOfRangeException) { 
            // Log then,
            throw ex;
        }
        if (ex is InvalidOperationException) {
            // Show message then,
            throw ex;
        }
        // and so on.
    }
}

如果在主线中使用try和catch,那么我会使用throw;重新抛出错误。 但是在上面的简单代码中,所有异常都通过HandleException

是否抛出前任;在HandleException内部调用时,与调用throw有相同的效果?


当前回答

当你抛出ex时,抛出的异常就变成了“原始”异常。因此所有之前的堆栈跟踪都不会在那里。

如果你抛出了异常,异常就会沿着这一行进行,你会得到完整的堆栈跟踪。

其他回答

是的,这是有区别的。

throw ex resets the stack trace (so your errors would appear to originate from HandleException) throw doesn't - the original offender would be preserved. static void Main(string[] args) { try { Method2(); } catch (Exception ex) { Console.Write(ex.StackTrace.ToString()); Console.ReadKey(); } } private static void Method2() { try { Method1(); } catch (Exception ex) { //throw ex resets the stack trace Coming from Method 1 and propogates it to the caller(Main) throw ex; } } private static void Method1() { try { throw new Exception("Inside Method1"); } catch (Exception) { throw; } }

其他答案完全正确,但我认为这个答案提供了一些额外的细节。

想想这个例子:

using System;

static class Program {
  static void Main() {
    try {
      ThrowTest();
    } catch (Exception e) {
      Console.WriteLine("Your stack trace:");
      Console.WriteLine(e.StackTrace);
      Console.WriteLine();
      if (e.InnerException == null) {
        Console.WriteLine("No inner exception.");
      } else {
        Console.WriteLine("Stack trace of your inner exception:");
        Console.WriteLine(e.InnerException.StackTrace);
      }
    }
  }

  static void ThrowTest() {
    decimal a = 1m;
    decimal b = 0m;
    try {
      Mult(a, b);  // line 34
      Div(a, b);   // line 35
      Mult(b, a);  // line 36
      Div(b, a);   // line 37
    } catch (ArithmeticException arithExc) {
      Console.WriteLine("Handling a {0}.", arithExc.GetType().Name);

      //   uncomment EITHER
      //throw arithExc;
      //   OR
      //throw;
      //   OR
      //throw new Exception("We handled and wrapped your exception", arithExc);
    }
  }

  static void Mult(decimal x, decimal y) {
    decimal.Multiply(x, y);
  }
  static void Div(decimal x, decimal y) {
    decimal.Divide(x, y);
  }
}

如果你取消注释抛出arithExc;行,你的输出是:

Handling a DivideByZeroException.
Your stack trace:
   at Program.ThrowTest() in c:\somepath\Program.cs:line 44
   at Program.Main() in c:\somepath\Program.cs:line 9

No inner exception.

当然,您丢失了关于异常发生位置的信息。如果你用投掷;行,这就是你得到的:

Handling a DivideByZeroException.
Your stack trace:
   at System.Decimal.FCallDivide(Decimal& d1, Decimal& d2)
   at System.Decimal.Divide(Decimal d1, Decimal d2)
   at Program.Div(Decimal x, Decimal y) in c:\somepath\Program.cs:line 58
   at Program.ThrowTest() in c:\somepath\Program.cs:line 46
   at Program.Main() in c:\somepath\Program.cs:line 9

No inner exception.

这样好多了,因为现在你看到的是程序。Div方法,导致你的问题。但是仍然很难看出这个问题是来自try块中的第35行还是第37行。

如果你使用第三种选择,在一个外部异常中包装,你不会丢失任何信息:

Handling a DivideByZeroException.
Your stack trace:
   at Program.ThrowTest() in c:\somepath\Program.cs:line 48
   at Program.Main() in c:\somepath\Program.cs:line 9

Stack trace of your inner exception:
   at System.Decimal.FCallDivide(Decimal& d1, Decimal& d2)
   at System.Decimal.Divide(Decimal d1, Decimal d2)
   at Program.Div(Decimal x, Decimal y) in c:\somepath\Program.cs:line 58
   at Program.ThrowTest() in c:\somepath\Program.cs:line 35

特别地,你可以看到第35行导致了这个问题。然而,这需要人们搜索InnerException,在简单的情况下使用内部异常感觉有点间接。

在这篇博客文章中,他们通过调用(通过反射)Exception对象上的内部实例方法InternalPreserveStackTrace()来保存行号(try块的行)。但是像这样使用反射并不是很好(. net Framework可能会在没有任何警告的情况下改变它们的内部成员)。

不,这将导致异常具有不同的堆栈跟踪。只有在catch处理程序中使用不带任何异常对象的throw才会保持堆栈跟踪不变。

你可能想从HandleException返回一个布尔值,不管是否应该重新抛出异常。

Throw保存堆栈跟踪。假设Source1抛出Error1,它被Source2捕获,而Source2表示抛出,那么在堆栈跟踪中Source1 Error + Source2 Error将可用。

Throw ex不保留堆栈跟踪。因此Source1的所有错误将被清除,只有Source2的错误将被发送到客户端。

有时只是阅读的东西不清楚,建议看这个视频演示,以获得更清楚,在c# Throw vs Throw ex。

看这里:http://blog-mstechnology.blogspot.de/2010/06/throw-vs-throw-ex.html

把:

try 
{
    // do some operation that can fail
}
catch (Exception ex)
{
    // do some local cleanup
    throw;
}

它保留了异常堆栈信息

这叫做"重扔"

如果想要抛出新的异常,

throw new ApplicationException("operation failed!");

把交货:

try
{
    // do some operation that can fail
}
catch (Exception ex)
{
    // do some local cleanup
    throw ex;
}

它不会发送异常堆栈信息

这叫做“打破堆栈”

如果想要抛出新的异常,

throw new ApplicationException("operation failed!",ex);