Why catch and rethrow an exception in C#?

前端 未结 17 920
遥遥无期
遥遥无期 2020-11-22 14:56

I\'m looking at the article C# - Data Transfer Object on serializable DTOs.

The article includes this piece of code:

public static string Se         


        
17条回答
  •  误落风尘
    2020-11-22 15:09

    It depends what you are doing in the catch block, and if you are wanting to pass the error on to the calling code or not.

    You might say Catch io.FileNotFoundExeption ex and then use an alternative file path or some such, but still throw the error on.

    Also doing Throw instead of Throw Ex allows you to keep the full stack trace. Throw ex restarts the stack trace from the throw statement (I hope that makes sense).

提交回复
热议问题