1

I send httpRequest and get httpResponse in a try block. Now I really do not care what type of exception do I get for several reason.

try
     {
        var request = WebRequest.Create(new Uri("MyUrl")) as HttpWebRequest;
        request.GetResponse();
     }
catch(Exception)
    {
    }

FxCop wants me to give specific exception so in my case I need to create several catch blocks as follows:

NotSupportedException
ArgumentNullException
SecurityException
InvalidOperationException
ProtocolViolationException
NotSupportedException
WebException

How can I tackle this warning and avoid writing these many catch blocks at the same time?

om471987
  • 5,398
  • 5
  • 32
  • 40
  • 3
    "Now I really do not care what type of exception do I get for several reason." What are those reasons? Catching `Exception` is almost always the wrong thing to do (remember, the exception handler also runs for exceptions you aren't expecting -- `OutOfMemoryException`, `ThreadAbortException`, things like that), so perhaps if you can explain why you catch it, there's a better option. –  Apr 11 '12 at 21:13

4 Answers4

4

Similar to another question that was asked on this topic, if you want to catch Exception, you must have a final throw within the handler in order to prevent the warning from appearing.

From the MSDN documentation on this warning:

To fix a violation of this rule, catch a more specific exception, or re-throw the general exception as the last statement in the catch block.

That said, this wouldn't really help in your situation since you want to "swallow" the exception, not rethrow it. Looking at this list of six specific exceptions, I can't imagine that it would be a painful experience to take the time (seriously like five minutes) to handle each of them in the most recommended way possible. It's not like this happens very often, and WebRequest is just one of those rare instances.

If your application will have a significant lifetime, odds are that over time you are going to find yourself implementing handlers for each one of these exceptions. No one likes "a general error 405 occurred" error messages. This may not be KISS, but putting the groundwork in place will give you the benefit of avoiding catching all Exceptions (like OutOfMemoryException), and will also prevent the FxCop warning (and scorn from picky developers).

If you are generating a lot of WebRequests in your application, perhaps a helper class as follows would be useful that throws a single WebFetcherException and also has more helpful error messages. Here's an example:

public static class WebFetcher
{
    public WebResponse FetchFromUrl(Uri uri)
    {
        try
        {
            var request = WebRequest.Create(new Uri("MyUrl")) as HttpWebRequest;
            return request.GetResponse();
        }
        catch (NotSupportedException ex)
        {
            //you could customize the error messages to be more suitable for your
            //application, or leaving room for future error handling
            throw new WebFetcherException(ex.Message, ex);
        }
        catch (ArgumentNullException ex)
        {
            throw new WebFetcherException(ex.Message, ex);
        }
        catch (SecurityException ex)
        {
            throw new WebFetcherException(ex.Message, ex);
        }
        catch (ProtocolViolationException ex)
        {
            throw new WebFetcherException(ex.Message, ex);
        }
        catch (WebException ex)
        {
            throw new WebFetcherException(ex.Message, ex);
        }
        catch (InvalidOperationException ex)
        {
            throw new WebFetcherException(ex.Message, ex);
        }
    }

    public class WebFetcherException : Exception
    {
        public WebFetcherException(string message, Exception inner)
            : base(message, inner)
        { }
    }
}
Community
  • 1
  • 1
Kevin McCormick
  • 2,358
  • 20
  • 20
3

You can deactivate several warnings, I always deactivate this warning :-)

Philipp
  • 1,425
  • 1
  • 11
  • 23
  • I don't get it. FxCop is analyzing your code, not compiling or running it. – Philipp Apr 11 '12 at 21:07
  • +1. Deactivating the warning seems the simplest approach as his business needs don't require handling different types of exceptions differently. – David Apr 11 '12 at 21:13
2

fxCop warnings are suggestions, not mandates.

In other words, you're free to use your judgment as to whether or not to follow the guidelines and suggestions made by fxCop.

This particular suggestion is there because it's probable that you'd want to handle different types of errors differently. If that's not the case, you can ignore this rule so it doesn't clutter up your warnings list.

You know your business rules and coding standards better than fxCop. It's just making suggestions based on best practices that may or may not apply to your particular situation.

More info can be found here: http://msdn.microsoft.com/en-us/library/bb429303(VS.80).aspx

David
  • 72,686
  • 18
  • 132
  • 173
  • Hmm. Makes sense.. FxCop is dumber than I thought..:D +2.5 – om471987 Apr 11 '12 at 21:18
  • 2
    I'd not call it dumb. It just happens to be unable to read your mind and can only go by statistics. And for -most- people it makes sense to have a non-catch-all exception handling block. – Cornelius Oct 09 '12 at 08:51
-1

try leave it empty

try
{
        var request = WebRequest.Create(new Uri("http://localhost:59449/stwebapi/getstatus?userId=" + wwId)) as HttpWebRequest;
        request.GetResponse();
     }
catch
{

}

Need to say that this is very bad design thow, so its better to follow FxCop guidelines, or at least handle an exception, and log it (at least)

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thanks Tigran. But in that case it caches even more generic error which is System.Object – om471987 Apr 11 '12 at 20:59
  • 1
    You are *seriously* suggesting the OP adopt a pattern of empty catch clauses!? – Kirk Woll Apr 11 '12 at 21:00
  • @KirkWoll: I was seriosly yet writing that this is a bad design and do not follow that. Ma answer is for question. – Tigran Apr 11 '12 at 21:00
  • @KirkWoll: well, what you would advice on this question? Improve the code ? – Tigran Apr 11 '12 at 21:02
  • @DilBarDaga_Means_Attack4Glory: what is your error in *that* case ? – Tigran Apr 11 '12 at 21:05
  • @Tigran:Problem happens with Boxing and boxing and it hampers performance. – om471987 Apr 11 '12 at 21:09
  • @DilBarDaga_Means_Attack4Glory: sorry, what is relation between empty `try/catch` and boxing? – Tigran Apr 11 '12 at 21:10
  • @Tigran: Example, If WebException is caught, and there is generic catch block exception will be boxed to that generic type and again will be unboxed while reading it – om471987 Apr 11 '12 at 21:12
  • 1
    @DilBarDaga_Means_Attack4Glory That's not what boxing means, and nothing of the sort happens with reference types. –  Apr 11 '12 at 21:24