how to: handle exceptions, best practices

前端 未结 3 866
忘了有多久
忘了有多久 2021-02-11 00:54

need to implement a global error handling, so maybe you can help out with the following example...

i have this code:

public bool IsUserA         


        
3条回答
  •  情书的邮戳
    2021-02-11 01:35

    Two golden rules:

    1. Throw an exception if a method can't do what its name says it does.
    2. Don't catch exceptions unless you know exactly what to do with them.

    Remember that an exception indicates that something has gone wrong, and that particular something may not be what you think. (Out of memory, stack overflow, 3rd party service gone down, botched deployment resulting in missing assemblies, mis-configuration, security exceptions etc).

    With very few exceptions, the only place you should see Pokemon exception handling is at the topmost level of your code, where the exception should be published somewhere. For example, in the Application_Error method in your global.asax file.

    Here are some links for you that you may find helpful:

    • If Your Method Can't Do What Its Name Promises It Can, Throw
    • The two golden rules of exception handling
    • Exception-Driven Development
    • ELMAH - Error Logging Modules and Handlers for ASP.NET

提交回复
热议问题