473,396 Members | 1,714 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,396 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 2869
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.