Best practices for catching and re-throwing .NET exceptions

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

    If you throw a new exception with the initial exception you will preserve the initial stack trace too..

    try{
    } 
    catch(Exception ex){
         throw new MoreDescriptiveException("here is what was happening", ex);
    }
    

提交回复
热议问题