473,241 Members | 1,528 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

FxCop warnings about general catch clauses ...

I have some general catch clauses in my app as follows:

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(SystemException ex)
{
}

I analyzed the code with Microsoft FxCop.
The FxCop warned me that "Generic exceptions should not be caught".
Could anyone explain what is wrong with usage of general catch clauses?
Thanks.

Nov 16 '05 #1
3 2854
What I understood from the descrition of FxCop error is that if you catch
the general exception(Exception),
then even if the error is a system level error for example
OutOfmemoryException, your code will catch it even when
it can not handle the exception and take corrective steps.

Instead if this exception was not caught and went up to system level, then
system software can take some
corrective action.

So basically it says you should only catch what you can handle and you
expect your code to throw.

I hope this helps.

Manish Soni
"Steve" <St***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
I have some general catch clauses in my app as follows:

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(SystemException ex)
{
}

I analyzed the code with Microsoft FxCop.
The FxCop warned me that "Generic exceptions should not be caught".
Could anyone explain what is wrong with usage of general catch clauses?
Thanks.

Nov 16 '05 #2
"Steve" <St***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
I have some general catch clauses in my app as follows:

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(SystemException ex)
{
}

I analyzed the code with Microsoft FxCop.
The FxCop warned me that "Generic exceptions should not be caught".
Could anyone explain what is wrong with usage of general catch clauses?

FxCop is wrong. You can catch Exception, you just can't eat it. It's a
Catch and Release only item.

Generic exceptions should often be caught, but they shoud usually be
rethrown or wrapped and rethrown.

You might catch Exception in order to capture stack data (arguments) which
will dissappear when the exception propagates back up the stack.

eg:

void foo(string s)
{
try
{
..do domething
}
catch(Exception ex)
{
throw new MyAppException("foo failed with argument s=" + s,ex);
}
}

or

void foo(string s)
{
try
{
..do domething
}
catch(Exception ex)
{
Trace.WriteLine("foo failed with argument s=[" + s + "] because " +
ex.Message);
throw;
}
}
There are many situations where it is inappropriate or unnecessary to catch
Exception, but it's not a general rule. If you are running a SqlCommand,
you should catch SqlException, not Exception. And you should never catch
Exception and then try to recover. Exception includes things like
ExecutionEngineExeption, OutOfMemoryException, ThreadAbortException and
StackOverflowException. You should never try to recover from these.
David
Nov 16 '05 #3
Steve,

The design guidelines implemented as FxCop rules are intended mainly for
library assemblies. Although most are perfectly applicable to "end"
applications, some aren't. This happens to be one of them. Basically, the
rule is meant to prevent library authors from suppressing exceptions that
should be thrown up to the application that is using the library.

It's perfectly acceptable to exclude violations of this rule under the
following circumstances:
1. You're writing a "end" application, and your catch block is being used
to handle exceptions that have nowhere else to go by, for example,
displaying exception information to the user or logging the exception
details.

2. You're writing a library application and your catch block either...
a. rethrows the exception with a simple "throw;" (not "throw ex;"),
or
b. throws a new exception whose inner exception is the caught
exception.

HTH,
Nicole
"Steve" <St***@discussions.microsoft.com> wrote in message
news:A7**********************************@microsof t.com...
I have some general catch clauses in my app as follows:

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(Exception ex)
{
}

try
{
}
catch(SystemException ex)
{
}

I analyzed the code with Microsoft FxCop.
The FxCop warned me that "Generic exceptions should not be caught".
Could anyone explain what is wrong with usage of general catch clauses?
Thanks.

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: White Wolf | last post by:
Hi, I cannot believe my eyes, but so far I could not find out what is the name of the construct, which is built from one try block and the catch clauses (handlers). I am not looking for a...
3
by: Rasmus | last post by:
I VS 2005 beta 2 i have a solution with - a number of classes - a website - a httphandler - a http module I want to run fxcop on my class files - but cant find out how to enable it. I've...
2
by: orekinbck | last post by:
Hi There I am inheriting from DateTimePicker class to create a DateTimePicker control with a configurable back colour. I got the original code from http://dotnet.mvps.org/ then converted it to...
3
by: Velvet | last post by:
I ran FxCop on one of the components for my web site and the security rules what me to add " tags like the ones listed below: This breaks my ASP.NET application. So my question is,...
0
by: Trey Nash | last post by:
Hi all, I am currently building my project with all code analysis turned on. From what I can tell, it looks to be employing FxCop and PREfast. The problem is, I am getting the following...
4
by: Noah Roberts | last post by:
The following code does VERY strange things in the debugger. Is there anything wrong with the following code, UB or anything else? #include <iostream> #include <exception> #include <stdexcept>...
5
by: Peter Ritchie [C# MVP] | last post by:
I've purposely been ignoring a CA2122 warning in some C++ interop code I've been working on for quite some time. I've just recently had the cycles to investigate the warning. The warning message...
7
by: Dave | last post by:
Hi all, I'm using FxCop to go through my code and it's showing bad things in my code. It is saying that you should never ever catch a general exception, and since in my (apparent) foolishness...
0
by: Dansk | last post by:
Hi all, We are running FxCop on our projects, and we allow people to supress FxCop warnings thanks to attributes like I need to track which rules were removed and why. Is there w away to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.