Solved

Top Level Exception Handling

Posted on 2010-09-10
1
288 Views
Last Modified: 2012-05-10
I'm using the code analyser in Visual Studio 2010 to try to improve my code.  One thing it's complaining about is that I'm catching exceptions of type Exception.  It says I shouldn't do that because it can hide problems from the user of my library and can complicate debugging.  However, I'm not writing a library I'm writing a web app.

I typically have a try/catch block around all the code in each event handler so I can detect an error and display an error message next to whatever the user clicked.  Also, I log the caught exception.  Is this the proper way to do exception handling in web apps?  Or should I review my code to see which exceptions can be thrown (SqlException, IndexOutOfRangeException, ArgumentNullException would probably be the most common) and catch those and let general exceptions go?  I really don't want to do that since I'm using AJAX.  An uncaught exception would result in no content update and that little javascript error icon in the corner of the browser.  Maybe I could catch the error using the Page.Error event handler and display a generic error message somewhere on the web page.

Alternatively, is there a way to set a flag or something to tell the code analyzer that this isn't a library and that this top level code and to not show this error instead of clicking suppress error for each one?

Well, any advise would be helpful/


Thanks,
0
Comment
Question by:ScottMacMaster
1 Comment
 
LVL 14

Accepted Solution

by:
existenz2 earned 500 total points
ID: 33646067
It complains about that catch the generic Exception and indeed not a FormatException, CommunicationException whatever. In good practises you would only catch what you expect that can be thrown and for the rest you rely on the customerrors default error handling.

If you don't care about that, nor care about what kind of exception it is, it's fine to just catch all and display a message.

You can disable rules in code analyzers so that these messages don't show up anymore.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question