Solved

Top Level Exception Handling

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

776 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