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 7492903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:39:16+00:00 2026-05-29T16:39:16+00:00

The .NET Framework has a lot of pre-defined exceptions. For example, if an invalid

  • 0

The .NET Framework has a lot of pre-defined exceptions. For example, if an invalid parameter was passed to my method, I’m supposed to raise an ArgumentException (or ArgumentNullException, if the paramter was null). If a method is called that is invalid for the current object state, I’m supposed to raise an InvalidOperationException.

Is there some pre-defined exception for the “this code should never be reached” case? Here’s an example:

void myMethod() {
    int a;

    ... /* Some complex code that manipulates a. In the end, "a" can only be 1 or 2. */

    switch (a) {
        case 1: ...
        case 2: ...
        default:  // just an extra sanity check
            // Oops, we should never be here.
            // There's apparently some bug in the code above.
            throw new ThereIsABugInTheCodeException();
    }
}

I don’t think creating my own exception for such a rare case is justified. On the other hand, just throwing Exception is discouraged as well. I guess some kind of AssertionFailedException would be appropriate, but I did not find any in the .net Framework. (Note that Trace.Assert does not throw an exception, so that’s not an option either.)


EDIT: To clarify: a is NOT an argument. Let me rephrase the question to make it clearer: In the middle of a method I do a sanity check (just to make sure, it should not be necessary if the code in the beginning of the method is correct). I find out the sanity check fails and would like to throw an exception. Which one do I throw? I don’t think that ArgumentException is the correct answer, since ArgumentException should only be thrown if the caller of the method did something wrong. That is not the case here. The method itself realizes at runtime that it contains a bug.

  • 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-29T16:39:18+00:00Added an answer on May 29, 2026 at 4:39 pm

    Oddly enough the .NET Framework does not contain an exception to describe logical or internal errors. But on the other hand the .NET Framework should have a quality level where there shouldn’t be a need to throw exceptions for internal errors. Would you trust a .NET API call that in the documentation stated that it might throw an InternalErrorException? Remember that exceptions derived from SystemException are exceptions used by the framework. It can be useful to reuse these exceptions but the exceptions were created for use by the framework and not your framework/application.

    You can use ArgumentException, ArgumentNullException, ArgumentOufOfRangeException, NotSupportedException and InvalidOperationException in cases where your methods a called in an unexpected way.

    You can also create your own InternalErrorException.

    Microsofts more general solution to this problem is Code Contracts. Many contracts can be checked at compile time helping you produce better code and you can use Contract.Assume and Contract.Assert to verify conditions in your code. The rewriter will insert code that will throw a ContractException if the condition is false. This exception has been crafted in a special way (from the DevLabs Code Contracts documentation):

    The ContractException type is not a public type and is emitted as a nested private type into each assembly for which runtime contract checking is enabled. It is thus not possible to write catch handlers catching only ContractException. Contract exceptions can thus only be handled as part of a general exception backstop. The rationale for this design is that programs should not contain control logic that depends on contract failures, just like programs should not catch ArgumentNullException or similar validation exceptions.

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

Sidebar

Related Questions

Suppose I have an MSI, that has .NET Framework as a pre-requisite. I know
Microsoft claims that the .Net 3.5 framework has many speed improvements over 2.0. Is
Has the introduction of the .net framework made raw programming in COM and DCOM
Is there a way in the .NET framework to validate that a process has
Does anybody know if Microsoft has already decided which version of the .NET framework
In the ADO.Net Entity Framework, I have an object which has 4 references to
My application is targeted for Compact .Net 1.0 framework. The application has to check
I have a .NET assembly(3.5 framework) and it basically has a set of custom
DUPE: Any Yahoo YUI validation framework that I can implement in asp.net MVC? Has
I'm new to Objective-C but has a lot experience with Java and .NET. I'm

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.