Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 868375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:07:47+00:00 2026-05-15T10:07:47+00:00

In .NET, when catching exceptions, should I always catch derived exceptions (so not ArgumentException

  • 0

In .NET, when catching exceptions, should I always catch derived exceptions (so not ArgumentException but the derived types)?

Also:

If I am asked to use error codes, would this be in the constructor like so?:

throw new Exception(“4000”, ex);

Or a custom exception type with an errorcode property? (This may get confusing with exception types like SqlException which have error codes mapping to SQL Server errors).

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:07:48+00:00Added an answer on May 15, 2026 at 10:07 am
    1. Catch the broadest exception that you know how to handle.

      In general, this means you’ll be catching a pretty specific exception. And some exceptions, like ArgumentExceptions, shouldn’t be caught at all b/c they indicate a logic error as opposed to a runtime error. One place where I’ve found catching a broader exception useful is in the case of File I/O. An IOException can be a practical higher-level exception to catch.

    2. If you are asked to use error codes, you could get away with using the message property of an exception to wrap it, but I would never use that as a reason to avoid throwing an appropriately typed exception. This is because there are two separate concerns here:

      a. The error code is there to provide a specific piece of information that can be looked up in the case of a failure in the field. It should never be used to discriminate between exception types programmatically b/c the language has a specific facility designed for that: exception types.

      b. The appropriately typed exception is there to provide a programmatic way of distinguishing between exceptions. The language is designed for it, use it. Don’t ever throw a plain Exception.

      I would probably throw an error code into the Exception.Data collection. This avoids overwriting messages in Exception.Message that would otherwise be very helpful for diagnostic purposes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What caching mechanism should I use for a .Net web application? Memcached seems to
I use partial page caching with ASP.NET. I find it to be particularly helpful
When catching an exception in .NET, you can have as many type-specific exception blocks
I have inherited some code that is not working as I think it should:
I have this snippet of code to deal with catching particular exceptions private static
java.net.ConnectException extends java.net.SocketException If I do the following, will it cater for both exceptions?
How can I verify a given xpath string is valid in C#/.NET? I'm not
I have been writing .NET applications and have been impressed with the error handling
ASP.NET Image Caching Problem (How to Disable it?) .IE browser load old image from
i just wondering how the .net wcf application caching is implemented ?? It's single

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.