Solved

Top Level Exception Handling

Posted on 2010-09-10
1
275 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now