TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

That Shouldn't Happen – UnreachableException in .NET 7

84 点作者 icey超过 2 年前

14 条评论

Mindless2112超过 2 年前
The state machine example is a good one. It&#x27;s a textbook case of unreachable code.<p>The null-checking example is not good. If the external API returns a null where you don&#x27;t expect one, UnreachableException will be thrown. It doesn&#x27;t matter if the documentation says it will never be null; clearly the code isn&#x27;t unreachable. All you need to reach it is bad data! (InvalidDataException would be a better choice, though arguably not an ideal one.)<p>If you can write a unit test that makes your code throw UnreachableException, you almost certainly should be throwing a different exception.
评论 #33271968 未加载
评论 #33274540 未加载
评论 #33271050 未加载
dustedcodes超过 2 年前
&gt; Fortunately, .NET 7 adds a new type to help us out here: UnreachableException.<p>There was an UnreachableException before .NET 7 as well.<p>Look:<p>public class UnreachableException : Exception {}
评论 #33272579 未加载
insaider超过 2 年前
Just wanted to highlight a relevant tool i recently discovered: Sentry.<p>It&#x27;s already become invaluable in my .net apps for tracking down tricky errors, especially in the new MAUI apps.<p>Caught one just yesterday where the app was trying to call an api with decimals serialized with a comma because it was on a dutch user&#x27;s phone! I imagine it would&#x27;ve been quite the headache catching that without sentry. Integrates with most tech-stacks too, not just .net btw
评论 #33270820 未加载
评论 #33270981 未加载
评论 #33272683 未加载
fabian2k超过 2 年前
For null checking I&#x27;d use the relatively new ArgumentNullException.ThrowIfNull, that automatically adds the parameter name in the exception and is a very compact and obvious way to check for null in your method parameters.<p>For enums I use ArgumentExceptions, something called this code with an invalid enum argument. I think there are some cases where Unreachable is a better error, but I&#x27;d probably be more inclined to use that for logic errors, where the code itself should not be reachable according to your understanding and reaching it would mean the code is wrong. The enums and null are cases where the arguments are unexpected, which to me is different.
Someone超过 2 年前
I don’t think that’s a good name for this. Clearly, that code can be reached. From the name, I would expect <i>”throw new UnreachableException”</i> to be something the compiler inserts to make it more robust in the sense that, if the compiler has some bug in its control flow analysis, it throws, rather than executes the wrong code.<p>Now, for a better name? Maybe <i>ShouldNeverGetHereException</i>, but that may be seen as too whimsical.
评论 #33272165 未加载
评论 #33272498 未加载
评论 #33275316 未加载
评论 #33272420 未加载
评论 #33272253 未加载
thom超过 2 年前
Ignoring the subject matter entirely, it&#x27;s always fun to see some snippets of C# du jour and marvel at the number of new syntactical features added since my last encounter.
评论 #33275265 未加载
keithnz超过 2 年前
seems to me they are using unreachable to mean unsupported. Also they should put a message in for the first example which should highlight the int value as that is the very first thing you are going to want to know.
WirelessGigabit超过 2 年前
If TeamId is a non-nullable string then the construction of that object should fail when TeamId is null, not at the consumption site.<p>This is exactly the issue what Rust is solving. A struct construction completes or fails. There is no weird in between state.
评论 #33272191 未加载
评论 #33271430 未加载
bob1029超过 2 年前
I think I still prefer NotImplementedException. It also covers things that <i>are</i> logically reachable but not ready for use yet. The nuance is the string argument to the ctor.
评论 #33272648 未加载
评论 #33272382 未加载
评论 #33272312 未加载
DeathArrow超过 2 年前
It&#x27;s not like you can&#x27;t define your own exceptions in C#, so you always could do what the article is saying.<p>Even if exceptions might be handy, I wouldn&#x27;t use exceptions for error handling for permance reasons. Instead I have another recommendation: never have void methods and use a Result type that packages the actual value along with an IsValid and Error property. So you will always return Result&lt;T&gt;.<p>That way, error handling is easy and doesn&#x27;t cause much performance overhead.
评论 #33271731 未加载
评论 #33271455 未加载
hardware2win超过 2 年前
C# needs closed enums
评论 #33272667 未加载
VoidWhisperer超过 2 年前
Off-topic slightly: does anything similar to what they are doing with that require extension method exist in Kotlin?
评论 #33271591 未加载
kagaw超过 2 年前
Is it more helpful if you will create a custom exception instead? E.g. class UnsupportedStateException : Exception
评论 #33272239 未加载
评论 #33271738 未加载
nomercy400超过 2 年前
Is this like java.lang.UnsupportedOperationException?
评论 #33271766 未加载
评论 #33272611 未加载