Exception Handling in .net web apps

后端 未结 6 740
盖世英雄少女心
盖世英雄少女心 2021-01-31 12:36

I admit it: I don\'t bother with too much exception handling. I know I should do more but I can never wrap my head around where to start and where to stop. I\'m not being lazy.

6条回答
  •  [愿得一人]
    2021-01-31 13:16

    The golden rule with exception handling is:

    "Only catch what you know how to handle"

    I've seen too many try-catch blocks where the catch does nothing but rethrow the exception. This adds no value. Just because you call a method that has the potential to throw an exception doesn't mean you have to deal with the possible exception in the calling code. It is often perfectly acceptable to let exceptions propagate up the call stack to some other code that does know what to do. In some cases, it is valid to let exceptions propagate all the way up to the user interface layer then catch and display the message to the user. It might be that no code is best-placed to know how to handle the situation and the user must decide the course of action.

提交回复
热议问题