简体   繁体   中英

Does Global Exception Handlers imply removal of unnecessary try catch blocks?

I have few basic doubts regarding use of Global Exception Handler(Application_Error/Page_Error) in ASP.NET and WCF(IErrorHandler) specifically.

If I use any of the above, will every exception pass through them and can I log and map them according to my will? also does that promote removing all unnecessary try catch blocks and replacing them with try finally wherever needed?

By 'unnecessary' I mean, catch blocks which eventually become redundant. eg If I'm catching all DB Exceptions and converting them to specific FaultException in wcf and now I'm doing same conversion in ProvideFault method, shouldn't I now remove those catch blocks as only job they were doing was to create FaultException with client friendly details?

Yes you can log errors globally, but it will not remove the need for you to do error handling in your pages (side note, some errors, like StackOverflowException, will not get caught). The only errors getting logged at the global level should be unexpected, not a way for you to avoid using try/catch. Here is a simple example of why:

You ask the user to enter a number. You parse the input as an int. They had input a string. Do you want to log this error at the global level? No. You'd want to catch it and display and error to the users.

Also, search NuGet for Elmah and see if that will work for you. Error handling in 5 minutes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM