admin管理员组

文章数量:1334951

I have the following code in a shared component:

public class MyClass {

    public void DoWork() {

        // ...

        if (someConditionWhichShouldAlwaysBeFalse) {
            throw new Exception("If we get here, there must be a bug in MyClass.");
        }        
    }    
}

I'm wondering if there's an appropriate built-in exception type in .NET that indicates:

  1. This exception is boneheaded, i.e. it must indicate a bug in my code
  2. It is not caused by any illegal caller input. The caller is using MyClass perfectly legally, and the error is entirely caused by bugs within MyClass

In a way, I just want a stronger, exception-based version of Debug.Assert(). The purpose is entirely to catch bugs within my code and I expect callers not to catch the exception (except maybe for logging and rethrow).

Is there a built-in type in the framework that suits this purpose? I've seen people use InvalidOperationException sometimes but my understanding is that IOE should be used for caller input which is inconsistent with object state (i.e. it is still fundamentally a caller error).

I have the following code in a shared component:

public class MyClass {

    public void DoWork() {

        // ...

        if (someConditionWhichShouldAlwaysBeFalse) {
            throw new Exception("If we get here, there must be a bug in MyClass.");
        }        
    }    
}

I'm wondering if there's an appropriate built-in exception type in .NET that indicates:

  1. This exception is boneheaded, i.e. it must indicate a bug in my code
  2. It is not caused by any illegal caller input. The caller is using MyClass perfectly legally, and the error is entirely caused by bugs within MyClass

In a way, I just want a stronger, exception-based version of Debug.Assert(). The purpose is entirely to catch bugs within my code and I expect callers not to catch the exception (except maybe for logging and rethrow).

Is there a built-in type in the framework that suits this purpose? I've seen people use InvalidOperationException sometimes but my understanding is that IOE should be used for caller input which is inconsistent with object state (i.e. it is still fundamentally a caller error).

Share Improve this question asked Nov 20, 2024 at 6:13 scharnywscharnyw 2,6862 gold badges23 silver badges35 bronze badges 6
  • "Rather, you should write your code so that the exception cannot possibly happen in the first place" - This is not an exception that should be implemented, it is just an exception type/classification based on code behavior. So when it appears ....for example a runtime wrong cast and your code is crashing.... now u have a "Boneheaded" exception. – D A Commented Nov 20, 2024 at 6:21
  • @DA How about cases where I want to throw such a boneheaded exception, to indicate a bug in my code? For example in the middle of a computation algorithm, it should always be the case that a > b, otherwise it indicates a bug in my algorithm. I just want to add a check if (a <= b) throw new Exception(); to catch bugs. Is there an appropriate built-in exception type that I can use here? – scharnyw Commented Nov 20, 2024 at 6:26
  • 1 Don't throw exceptions to catch bugs, write tests instead. – MindSwipe Commented Nov 20, 2024 at 7:06
  • 2 I'd prefer Trace.Assert(!cond, message); – shingo Commented Nov 20, 2024 at 7:06
  • 1 stackoverflow/questions/12682432/… – Hans Passant Commented Nov 20, 2024 at 17:18
 |  Show 1 more comment

1 Answer 1

Reset to default 2

Sounds to me like UnreachableException is what you're looking for, introduced in .NET 7:

The exception that is thrown when the program executes an instruction that was thought to be unreachable

本文标签: cIs there any builtin exception type for a boneheaded exception in NETStack Overflow