Best practices for catching and re-throwing .NET exceptions

前端 未结 11 1615
野的像风
野的像风 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

    I would definitely use:

    try
    {
        //some code
    }
    catch
    {
        //you should totally do something here, but feel free to rethrow
        //if you need to send the exception up the stack.
        throw;
    }
    

    That will preserve your stack.

提交回复
热议问题