Exception Handling in .net web apps

后端 未结 6 743
盖世英雄少女心
盖世英雄少女心 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:22

    Might be more about exception handling in general than ASP.NET speific but:

    • Try to catch exceptions as close to the cause as possible so that you can record (log) as much information about the exception as possible.
    • Include some form of catch all, last resort exception handler at the entry points to your program. In ASP.NET this could be the Application level error handler.
    • If you don't know how to "correctly" handle an exception let it bubble up to the catch all handler where you can treat it as an "unexpected" exception.
    • Use the Try***** methods in .NET for things like accessing a Dictionary. This helps avoid major performance problems (exception handling is relatively slow) if you throw multiple exceptions in say a loop.
    • Don't use exception handling to control normal logic of your program, e.g. exiting from a loop via a throw statement.

提交回复
热议问题