Solved

Top Level Exception Handling

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
FInd Image Control Gridview 3 21
Import MySQL data into MS Access using VB.Net interface 5 29
Convert Ctime to date time in textfile? 7 31
Name Space error VS2015 1 24
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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