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:
- This exception is boneheaded, i.e. it must indicate a bug in my code
- It is not caused by any illegal caller input. The caller is using
MyClass
perfectly legally, and the error is entirely caused by bugs withinMyClass
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:
- This exception is boneheaded, i.e. it must indicate a bug in my code
- It is not caused by any illegal caller input. The caller is using
MyClass
perfectly legally, and the error is entirely caused by bugs withinMyClass
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).
1 Answer
Reset to default 2Sounds 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
版权声明:本文标题:c# - Is there any built-in exception type for a boneheaded exception in .NET? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742377881a2463527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
a > b
, otherwise it indicates a bug in my algorithm. I just want to add a checkif (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:26Trace.Assert(!cond, message);
– shingo Commented Nov 20, 2024 at 7:06