两者有什么区别

try { ... }
catch{ throw }

and

try{ ... }
catch(Exception e) {throw new Exception(e.message) }

不管第二个显示的是一条消息。


当前回答

Throw或Throw ex,两者都用于抛出或重新抛出异常,当你只是简单地记录错误信息而不想向调用者发送任何信息时,你只需在catch和leave中记录错误。

但是,如果你想向调用者发送一些关于异常的有意义的信息,你可以使用throw或throw ex。现在,throw和throw ex之间的区别是,throw保留了堆栈跟踪和其他信息,但throw ex创建了一个新的异常对象,因此原始的堆栈跟踪丢失了。

什么时候应该使用throw和throw e呢?在一些情况下,您可能需要重新抛出异常,例如重置调用堆栈信息。

例如,如果方法在库中,而您希望对调用代码隐藏库的详细信息,则不一定希望调用堆栈包含关于库中私有方法的信息。在这种情况下,您可以捕获库的公共方法中的异常,然后重新抛出它们,以便调用堆栈从这些公共方法开始。

其他回答

throw重新抛出捕获的异常,保留堆栈跟踪,而throw new exception丢失捕获的异常的一些细节。

通常使用throw本身来记录异常,而不完全处理它。

BlackWasp有一篇很好的文章,标题是c#中抛出异常。

这里的答案都没有显示出差异,这可能有助于那些努力理解差异的人。考虑下面的示例代码:

using System;
using System.Collections.Generic;

namespace ExceptionDemo
{
   class Program
   {
      static void Main(string[] args)
      {
         void fail()
         {
            (null as string).Trim();
         }

         void bareThrow()
         {
            try
            {
               fail();
            }
            catch (Exception e)
            {
               throw;
            }
         }

         void rethrow()
         {
            try
            {
               fail();
            }
            catch (Exception e)
            {
               throw e;
            }
         }

         void innerThrow()
         {
            try
            {
               fail();
            }
            catch (Exception e)
            {
               throw new Exception("outer", e);
            }
         }

         var cases = new Dictionary<string, Action>()
         {
            { "Bare Throw:", bareThrow },
            { "Rethrow", rethrow },
            { "Inner Throw", innerThrow }
         };

         foreach (var c in cases)
         {
            Console.WriteLine(c.Key);
            Console.WriteLine(new string('-', 40));
            try
            {
               c.Value();
            } catch (Exception e)
            {
               Console.WriteLine(e.ToString());
            }
         }
      }
   }
}

生成如下输出:

Bare Throw:
----------------------------------------
System.NullReferenceException: Object reference not set to an instance of an object.
   at ExceptionDemo.Program.<Main>g__fail|0_0() in C:\...\ExceptionDemo\Program.cs:line 12
   at ExceptionDemo.Program.<>c.<Main>g__bareThrow|0_1() in C:\...\ExceptionDemo\Program.cs:line 19
   at ExceptionDemo.Program.Main(String[] args) in C:\...\ExceptionDemo\Program.cs:line 64

Rethrow
----------------------------------------
System.NullReferenceException: Object reference not set to an instance of an object.
   at ExceptionDemo.Program.<>c.<Main>g__rethrow|0_2() in C:\...\ExceptionDemo\Program.cs:line 35
   at ExceptionDemo.Program.Main(String[] args) in C:\...\ExceptionDemo\Program.cs:line 64

Inner Throw
----------------------------------------
System.Exception: outer ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at ExceptionDemo.Program.<Main>g__fail|0_0() in C:\...\ExceptionDemo\Program.cs:line 12
   at ExceptionDemo.Program.<>c.<Main>g__innerThrow|0_3() in C:\...\ExceptionDemo\Program.cs:line 43
   --- End of inner exception stack trace ---
   at ExceptionDemo.Program.<>c.<Main>g__innerThrow|0_3() in C:\...\ExceptionDemo\Program.cs:line 47
   at ExceptionDemo.Program.Main(String[] args) in C:\...\ExceptionDemo\Program.cs:line 64

正如前面的回答中所指出的,裸抛出清楚地显示了失败的原始代码行(第12行)以及异常发生时调用堆栈中活动的另外两个点(第19行和第64行)。

重新抛出案例的输出显示了为什么这是一个问题。当像这样重新抛出异常时,异常将不包括原始堆栈信息。注意,只包括throw e(第35行)和最外层的调用堆栈点(第64行)。如果以这种方式抛出异常,则很难将fail()方法作为问题的根源。

最后一种情况(innerThrow)是最详细的,包含的信息比上面任何一种都要多。因为我们正在实例化一个新的异常,所以我们有机会添加上下文信息(这里是“外部”消息,但我们也可以在新异常上添加到. data字典中),以及保留原始异常中的所有信息(包括帮助链接、数据字典等)。

第一个保存原始的stacktrace:

try { ... }
catch
{
    // Do something.
    throw;
}

第二个选项允许您更改异常和/或消息和其他数据的类型:

try { ... } catch (Exception e)
{
    throw new BarException("Something broke!");
}

还有第三种传递内部异常的方法:

try { ... }
catch (FooException e) {
    throw new BarException("foo", e);
} 

我建议使用:

如果您想在错误情况下做一些清理,而不破坏信息或添加关于错误的信息,则使用第一种方法。 如果您想添加关于错误的更多信息,则使用第三个。 如果您想隐藏信息(不受信任的用户),则使用第二种方法。

抛出一个新的异常会破坏当前的堆栈跟踪。

扔;将保留原始的堆栈跟踪,并且几乎总是更有用。该规则的例外情况是当您希望将异常包装在自己的自定义异常中时。你应该这样做:

catch(Exception e)
{
    throw new CustomException(customMessage, e);
}

Throw或Throw ex,两者都用于抛出或重新抛出异常,当你只是简单地记录错误信息而不想向调用者发送任何信息时,你只需在catch和leave中记录错误。

但是,如果你想向调用者发送一些关于异常的有意义的信息,你可以使用throw或throw ex。现在,throw和throw ex之间的区别是,throw保留了堆栈跟踪和其他信息,但throw ex创建了一个新的异常对象,因此原始的堆栈跟踪丢失了。

什么时候应该使用throw和throw e呢?在一些情况下,您可能需要重新抛出异常,例如重置调用堆栈信息。

例如,如果方法在库中,而您希望对调用代码隐藏库的详细信息,则不一定希望调用堆栈包含关于库中私有方法的信息。在这种情况下,您可以捕获库的公共方法中的异常,然后重新抛出它们,以便调用堆栈从这些公共方法开始。