Best practices for catching and re-throwing .NET exceptions

前端 未结 11 1710
野的像风
野的像风 2020-11-22 07:15

What are the best practices to consider when catching exceptions and re-throwing them? I want to make sure that the Exception object\'s InnerException

11条回答
  •  天命终不由人
    2020-11-22 08:06

    You should always use "throw;" to rethrow the exceptions in .NET,

    Refer this, http://weblogs.asp.net/bhouse/archive/2004/11/30/272297.aspx

    Basically MSIL (CIL) has two instructions - "throw" and "rethrow":

    • C#'s "throw ex;" gets compiled into MSIL's "throw"
    • C#'s "throw;" - into MSIL "rethrow"!

    Basically I can see the reason why "throw ex" overrides the stack trace.

提交回复
热议问题