TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

This should never happen

626 pointsby qprofyehabout 9 years ago

58 comments

FiatLuxDaveabout 9 years ago
My favorite example of a &quot;this should never happen&quot; error was when I got a call from a customer, who started the conversation by asking, &quot;Who is Brian?&quot;.<p>I was caught a bit off guard, but I assumed the customer must know someone at the company, since Brian was the name of the previous electrical engineer&#x2F;firmware programmer. So, I told them that Brian didn&#x27;t work here any more, but was there anything that I could help them with? The customer said, &quot;Well, the device says that I should call Brian&quot;. I was confused by this, and asked a lot of questions until I determined that the device was actually displaying &quot;CALL BRIAN&quot; on the LCD display.<p>This was quite unusual, and at first I didn&#x27;t believe the customer, until he sent a picture of the device showing the message.<p>So, I dug into the code, and quickly found the &quot;Call Brian&quot; error condition. It was definitely one of those &quot;this should never happen&quot; cases. I presume that Brian had put that in during firmware development to catch an error case he was afraid might happen due to overwriting valid memory locations.<p>I got the device back, and found out that the device had a processor problem (I don&#x27;t remember exactly what) that would write corrupted data to memory. So, really, it should never happen.<p>That particular device has now been in production for 10 years, and that is the only time that error has ever appeared.
评论 #11397370 未加载
评论 #11398924 未加载
评论 #11397440 未加载
评论 #11397479 未加载
评论 #11404264 未加载
评论 #11399984 未加载
评论 #11398542 未加载
dzdtabout 9 years ago
&quot;This should never happen&quot; is a design pattern of defensive programming. This is the same pattern for assert.<p>The usual use is to catch errors caused by misuse of a method. There is some invariant that the method assumes but is not enforced by the type signature of the interface. So if something goes wrong in outside code, or someone tries to use the method incorrectly, the invariant is not satisfied. When you catch such a problem, the current code context is FUBAR. The question is how aggressively to bail out : spew errors to a log and proceed with some GIGO calculation? Throw an exception? Exit the program?
评论 #11396934 未加载
评论 #11396912 未加载
评论 #11397425 未加载
评论 #11397756 未加载
paulkaplanabout 9 years ago
Using github to search like this reminds me of how a CS professor of mine would show the &quot;best commit messages of the year&quot; (homework was submitted via git) by looking for various patterns like all caps, all symbols, etc.<p><a href="http:&#x2F;&#x2F;www.slideshare.net&#x2F;bsotomay&#x2F;uchicago-cmsc-23300-the-best-commit-messages-of-2015" rel="nofollow">http:&#x2F;&#x2F;www.slideshare.net&#x2F;bsotomay&#x2F;uchicago-cmsc-23300-the-b...</a>
评论 #11398253 未加载
评论 #11397359 未加载
评论 #11397122 未加载
评论 #11396804 未加载
评论 #11396785 未加载
评论 #11396802 未加载
评论 #11396636 未加载
评论 #11399227 未加载
评论 #11396792 未加载
评论 #11396915 未加载
BinaryIdiotabout 9 years ago
My favorite part is the Java project that has an exception class called ThisShouldNeverHappenException [1]. Only in Java would someone create an exception class for a condition that should never happen :)<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;TheProjecter&#x2F;propidle&#x2F;blob&#x2F;f0d5320e2a3d46f9fe7ea3c36fc071256e023234&#x2F;src&#x2F;com&#x2F;googlecode&#x2F;propidle&#x2F;util&#x2F;ThisShouldNeverHappenException.java" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;TheProjecter&#x2F;propidle&#x2F;blob&#x2F;f0d5320e2a3d46...</a>
评论 #11396359 未加载
评论 #11396987 未加载
评论 #11396691 未加载
评论 #11396444 未加载
评论 #11397147 未加载
评论 #11396964 未加载
wimaggucabout 9 years ago
GitHub&#x27;s search is pretty interesting: every time I refresh the search page it shows a different number of results: 18,401,830; 17,751,631; 15,995,799.<p>Which is anyways quite a lot of results, but then this search finds <i>ThisShouldNeverHappenException</i>, the string <i>&quot;this should never happen&quot;</i> and stuff like<p><pre><code> &#x2F;&#x2F; *This* gets run before every test. if (b &gt; d) { fail(&quot;XX *should never happen*&quot;); } </code></pre> With quotations it&#x27;s <i>only</i> about 500,000.
评论 #11396419 未加载
评论 #11396480 未加载
评论 #11396489 未加载
useaabout 9 years ago
Sometimes you have to satisfy the compiler because it has less information about a situation than you do. Ideally this would be captured in the type system, but limitations (language, project, politics, time, etc) may prevent this. As another comment mentions, it&#x27;s a kind of invariant check.<p>For example, based on external information you might know[1] that a condition inside of a loop will always be hit exactly one time. Your compiler or tools might not be able to determine that same thing. It may try to force you to do something like assign a value or whatever you did in that condition, that it can&#x27;t guarantee has happened. In such a scenario, it might[2] make sense to have something like &quot;this should never happen&quot; after the loop, with a brief comment explaining why you&#x27;ve done this.<p>[1] I think this is the crux of the issue. We programmers often think we &quot;know&quot; something, but it might be an incorrect assumption. IMO part of being a good programmer is examining your assumptions at every step. The chasm is vast, between &quot;the framework strongly guarantees X&quot; and &quot;the function that gets called before this one has done X already&quot;. The former is OK if you want to get work done, while the latter is much more brittle and possibly dangerous, depending on the level of coupling you&#x27;re willing to accept.<p>[2] Nine times out of ten, a reorganization of the logic makes more sense. However, I do think there are scenarios where this pattern is the best choice given the options.
userbinatorabout 9 years ago
In my experience, &quot;this should never happen&quot; cases often are a sign of very brittle design that branches into many separate but nearly-identical paths, and could be simplified to remove them. The other thing it points to is bad error handling paths (assuming that an error could &quot;never happen&quot;.)<p>Also funny to see Java being the most verbose as usual, with its ThisShouldNeverHappenException.java
评论 #11396324 未加载
评论 #11396424 未加载
评论 #11396833 未加载
ZeroGravitasabout 9 years ago
Apparently things-that-shouldn&#x27;t-happen happen 10x more in C code than the next nearest language.
评论 #11396344 未加载
评论 #11396593 未加载
评论 #11396309 未加载
cbaleanuabout 9 years ago
This too: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=no+idea+why+this+works&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=no+idea+why+this+...</a><p>:)
davesqueabout 9 years ago
Not trolling here. I&#x27;m curious why this is getting so much attention. Isn&#x27;t saying &quot;this should never happen&quot; just making an assertion about the behavior of your code? As far as the apparent inconsistencies in the code seen in the results go, is it really fair to judge a random piece of code from a random person (and completely out of context)? Don&#x27;t we all have scratch&#x2F;experimental&#x2F;incomplete code hosted on our github accounts?
codeulikeabout 9 years ago
But what they mean is, &#x27;This should never happen unless there&#x27;s a serious problem upstream&#x27; and so these sorts of Asserts and Throws are actually very useful. Its all about &#x27;Fail Fast&#x27;
daniel_iversenabout 9 years ago
I think thats just good common practice - the amounts of times I&#x27;ve seen that message when obviously I never expected to :)<p>This one is more worrying I suppose: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=&amp;q=FIXME&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=&amp;q=FIXME&amp;type=Code&amp;ref=search...</a>
atemerevabout 9 years ago
I usually annotate should-never-happen asserts by the most plausible explanation how it _could_ happen, for the sake of some poor guy unlucky enough for having to debug my code.<p>E.g. &quot;Should not happen — probably a bug in Apache Commons Math?&quot;<p>Or &quot;Shouldn&#x27;t really happen, barring compiler bugs or cosmic rays&quot;<p>As they told us, there is no such thing as probability of zero.
pjmorrisabout 9 years ago
call error(&quot;in omatch: can&#x27;t happen&quot;); - Line from omatch routine on pg. 146 [1].<p>Snips from discussion in [1]: &quot;We can expect problems, therefore, and should prepare for them. ... Garbage is bad enough, but garbage which is expected to contain a count to tell you how long it is can be much worse. ... The first time we ran this code it said &#x27;can&#x27;t happen&#x27; We got that message perhaps a hundred times in the process of adding the rest of the code... This experience speaks for itself; if you&#x27;re going to walk a high-wire, use a net.&quot;<p>&#x27;Can&#x27;t happen&#x27; is as much a pattern as &#x27;Hello, World&#x27;... and it has the same genesis.<p>[1] Brian Kernighan, P.J. Plauger, &#x27;Software Tools&#x27;, Addison-Wesley 1976
saint-loupabout 9 years ago
Previous noteworthy Github searches, found via hn.algolia.com :<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?q=ugly+hack&amp;ref=cmdform&amp;type=Code" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?q=ugly+hack&amp;ref=cmdform&amp;type=Code</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=filename%3Aid_rsa&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=filename%3Aid_rsa...</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?p=3&amp;q=extension%3Aphp+mysql_query+%24_GET&amp;ref=searchresults&amp;type=Code" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?p=3&amp;q=extension%3Aphp+mysql_query+...</a>
paul_milovanovabout 9 years ago
Search for &quot;WTF&quot;, results by language<p>C++ 2.4M C 400K Java 150K ...<p>Sounds about right.
评论 #11396810 未加载
cptskippyabout 9 years ago
This is what you throw when your padLeft webservice call fails.
smoyerabout 9 years ago
One of the most common causes of failures are cases that the programmer never considered. Once of my favorite test coverage tools shows you missed branches and I find that invaluable.<p>My initial reaction was &quot;oh no&quot; but as I thought about it, this explicitly indicates that the programmer actually thought about a case.<p>And is it any different than what most of us do in our unit tests? If we&#x27;re expecting an exception that isn&#x27;t thrown or the wrong exception is thrown, we force a test failure.<p>One of the goals our team is working towards is more robust and complete metric and log collection. We specifically want to capture exceptions that make it to the application server for analysis, but this assumes that the developer has a) considered all cases and b) caught intermediate exceptions and continued processing (or abort).
maxafabout 9 years ago
There are better (aka more creative) ways of inducing rage in fellow cow-orkers. <a href="https:&#x2F;&#x2F;twitter.com&#x2F;noahlz&#x2F;status&#x2F;709961243277332482" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;noahlz&#x2F;status&#x2F;709961243277332482</a>
bryanrasmussenabout 9 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Why+does+this+happen&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Why+does+this+hap...</a>
评论 #11396343 未加载
w8rbtabout 9 years ago
<p><pre><code> void(int a, int b) { const int i = 0; int result = a + b; if (i &gt; 0) { &#x2F;&#x2F; This will never happen ;) result = result &#x2F; 0; } }</code></pre>
Stratoscopeabout 9 years ago
One product I worked on a number of years ago had a CantHappen() function with a simple implementation. It displayed this message box:<p><pre><code> You are not here. </code></pre> Another message was in the Mac installer when there wasn&#x27;t room to install:<p><pre><code> Your hard disk is too small. </code></pre> That&#x27;s the complete text of both messages, and yes, it displayed them <i>to the customer</i>. &lt;sigh&gt;<p>After seeing these, I started going through the code and found a bunch of other rude, confusing, or jargony messages. It actually turned into a fun little project cleaning these up!
yeukhonabout 9 years ago
&quot;wtf&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22wtf%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22wtf%22&amp;type=Co...</a><p>Anyway, if you read commits in Linux you&#x27;d find a lot of fuck in there. Quite amusing.<p>When I was an intern, I loved looking at MXR to find words like fuck: <a href="http:&#x2F;&#x2F;mxr.mozilla.org&#x2F;mozilla-central&#x2F;search?string=fuck" rel="nofollow">http:&#x2F;&#x2F;mxr.mozilla.org&#x2F;mozilla-central&#x2F;search?string=fuck</a>
kbensonabout 9 years ago
There&#x27;s almost twice as many &quot;should not get here&quot; results[1], which I think would mostly be used in the same situation, in case someone is looking for more.<p>1: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+here&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+he...</a>
insulanianabout 9 years ago
C#: 58355, F#: 464, Haskel: 6
评论 #11396988 未加载
protomythabout 9 years ago
I remember one place I worked had an error thrown that showed as hex code &quot;48 45 4C 4C 4E 4F&quot;. As far as I know it only occurred once in test when someone did something epically stupid as a patch to the code preceding it. We removed the patch and never saw the code again. Have no real clue who put it in there, as code control was a later addition.
pklauslerabout 9 years ago
Cray Research had a linker named SEGLDR that was written in Fortran, whose STOP statement allows an optional string message, and so it was used for run-time assertion checking like<p>IF(.NOT.CHECK()) STOP &#x27;xxx&#x27;<p>Anyway, somebody (not me) got into trouble when Very Serious Customers were offended by seeing the occasional STOP DAMN message at link time.
mictlannabout 9 years ago
&quot;Testing this shit&quot; -&gt; We’ve found 129,140 code results <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Testing+this+shit&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=Testing+this+shit...</a>
JohnTHallerabout 9 years ago
A related search I like is &quot;why does this work&quot;: <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22why+does+this+work%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22why+does+this+...</a>
nkhumphreysabout 9 years ago
I think &quot;should not get here&quot; highlights this point better<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+here&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=should+not+get+he...</a>
shimon_eabout 9 years ago
C still in the lead for: why is this happening <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=why+is+this+happening&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=why+is+this+happe...</a>
js2about 9 years ago
And the related Log.wtf() on Android:<p><a href="http:&#x2F;&#x2F;developer.android.com&#x2F;reference&#x2F;android&#x2F;util&#x2F;Log.html#wtf(java.lang.String,%20java.lang.Throwable)" rel="nofollow">http:&#x2F;&#x2F;developer.android.com&#x2F;reference&#x2F;android&#x2F;util&#x2F;Log.html...</a>
vitautabout 9 years ago
Since so many people obviously need this functionality, I&#x27;ve created a reusable Python version here: <a href="https:&#x2F;&#x2F;github.com&#x2F;vitaut&#x2F;neverhappen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;vitaut&#x2F;neverhappen</a>
Twisellabout 9 years ago
By far not as popular as foo <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=foo&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=foo&amp;type=Code&amp;ref...</a>
logicalleeabout 9 years ago
Maybe even more interesting:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22no+idea+why+this+works%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22no+idea+why+th...</a>
评论 #11397076 未加载
shade23about 9 years ago
I find this more entertaining : <a href="https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;search?utf8=%E2%9C%93&amp;q=fuck" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;torvalds&#x2F;linux&#x2F;search?utf8=%E2%9C%93&amp;q=fu...</a>
评论 #11396799 未加载
heyitskevinabout 9 years ago
I wish we could sort by most starred to see what popular repositories have code like this.
dc2about 9 years ago
<a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22this+is+so+broken%22&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=%22this+is+so+bro...</a>
jballancabout 9 years ago
Reminds me of the &quot;Six Stages of Debugging&quot;: <a href="http:&#x2F;&#x2F;plasmasturm.org&#x2F;log&#x2F;6debug&#x2F;" rel="nofollow">http:&#x2F;&#x2F;plasmasturm.org&#x2F;log&#x2F;6debug&#x2F;</a>
mdjtabout 9 years ago
if happens: print &quot;shit, that wasn&#x27;t supposed to happen&quot;
评论 #11396358 未加载
评论 #11399487 未加载
评论 #11396486 未加载
评论 #11396465 未加载
jroseattleabout 9 years ago
This is awesome.<p><pre><code> public class ThisShouldNeverHappenException extends RuntimeException { public ThisShouldNeverHappenException(Exception cause) { super(cause); } }</code></pre>
ninjakeyboardabout 9 years ago
IllegalStateException already exists. Why would people make their own?
xydacabout 9 years ago
Best one is<p>&#x2F;<i></i> * This should never happen exception. Use in situation that really shouldn&#x27;t happen...NEVER * * *&#x2F; public class NeverHappenException extends RuntimeException {
justspamjustinabout 9 years ago
todo: stuff <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+stuff&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+stuff&amp;type=C...</a> todo: meh <a href="https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+meh&amp;type=Code&amp;ref=searchresults" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;search?utf8=%E2%9C%93&amp;q=todo+meh&amp;type=Cod...</a>
ben_prabout 9 years ago
The shocking part is the number of times:<p>&quot;This should never happen&quot;: 823,044 This should never happen: 16,946,357 vs. &quot;This is screwed up&quot;: 59 This is screwed up: 876,393
onetimePeteabout 9 years ago
Could be that some of the &quot;This should never happen&quot; can be deduced by optimizing compilers and never exist in assembly? Not talking about the interpreted stuff.
z3t4about 9 years ago
At least make the error say WHY it should never happen.
raldiabout 9 years ago
If you work for a big company, search for this string in your bug-ticketing system to see example logs from all the times these lines are triggered.
INTPenisabout 9 years ago
I remember using the comment &quot;Craw! Polly shouldn&#x27;t be!&quot; instead, as a homage to a specific Simpsons episode. :)
nevirabout 9 years ago
The thing is, we should be applauding people for many of these assertions. At least when they&#x27;re throwing errors
dumaspereabout 9 years ago
This reminds me of the sage advice, &quot;Never check for a condition that you don&#x27;t know how to handle.&quot;
rubinho_about 9 years ago
The best thing is that someone made a &#x27;ThisShouldNeverHappenException&#x27;.<p>+1
cgtyoderabout 9 years ago
I prefer using CANTHAPPEN in the comment - easier to grep for.
nlyabout 9 years ago
I&#x27;m a big fan of &#x27;DO NOT MERGE&#x27; in git logs
评论 #11398485 未加载
danvideoabout 9 years ago
Why no Ruby results?
pgtruesdellabout 9 years ago
As usual, these submissions from Github searches like this are just sad.
ameliusabout 9 years ago
assert(false);
评论 #11396712 未加载
lilbobbytablesabout 9 years ago
aaand naturally javascript is right up there in the top languages where this is found.
评论 #11396749 未加载
评论 #11396930 未加载