Live data from Hacker News

This should never happen

github.com

21–30 of 214 posts

Re: This should never happen

#21
post #16

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 :) [1] https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...

Not just Java. I've seen similar classes in C++ and C# to indicate things which should never occur/are clearly bad programmer mistakes/... Think InternalErrorException/DevFailedError etc. Sometimes it's just a sane thing to do, and using such names means you don't need to write the dreaded 'should never happen' comment manually anymore.

To elaborate a bit, in C++ the relevant exception class is called `std::logic_error` (contrast with `std::runtime_error`). I like it. It is a bit more descriptive of the actual situation than "this should never happen".

Re: This should never happen

#22

GitHub'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. Which is anyways quite a lot of results, but then this search finds ThisShouldNeverHappenException , the string "this should never happen" and stuff like // *This* gets run before every test. if (b > d) { fail("XX *should never happen*"); } With quotations it's only a…

Usually, you'd never actually _count_ the expected results for such stuff. Instead, you'd return the estimate number of records (think SQL EXPLAIN). I'd gather the number seems very dynamic because of the constant stream of commits.

Re: This should never happen

#23

In my experience, "this should never happen" 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 "never happen".) Also funny to see Java being the most verbose as usual, with its ThisShouldNeverHappenException.java

I use "this should never happen" branches when creating new code fully expecting those branches to be hit so I can debug the case.

Those branches ought to disappear once the code and tests improve but we all know how that goes :-)

Re: This should never happen

#24
"This should never happen" is a design pattern of defensive programming. This is the same pattern for assert.

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?

Re: This should never happen

#25
post #16

Earlier quoted context omitted.

Not just Java. I've seen similar classes in C++ and C# to indicate things which should never occur/are clearly bad programmer mistakes/... Think InternalErrorException/DevFailedError etc. Sometimes it's just a sane thing to do, and using such names means you don't need to write the dreaded 'should never happen' comment manually anymore.

Isn't that what assertions are for?

Assertions can be disabled at runtime. If your goal is to call out the fact that something has gone fundamentally wrong with your program's state, an exception is the way to go.

Re: This should never happen

#26

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 :) [1] https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...

Exceptions in Java are to raise them. In Go there is a panic() call, which semantically sounds to me very much like ThisShouldNeverHappenException. It doesn't make Go worse and Java better though.

Re: This should never happen

#27
post #4

if happens: print "shit, that wasn't supposed to happen"

Never ever EVER put bad language in any unexpected error cases or logs - even as you're developing it... it WILL somehow magically make its way to production, and it WILL appear!

I remind myself frequently not to swear in code as I am quite juvenile on a normal day. If I'm struggling I'll just make up a word, so that if it ever happens that someone else sees it I can say it is an acronym but that I have conveniently forgotten for what.

Re: This should never happen

#30

Earlier quoted context omitted.

Never ever EVER put bad language in any unexpected error cases or logs - even as you're developing it... it WILL somehow magically make its way to production, and it WILL appear!

I remind myself frequently not to swear in code as I am quite juvenile on a normal day. If I'm struggling I'll just make up a word, so that if it ever happens that someone else sees it I can say it is an acronym but that I have conveniently forgotten for what.

You could always put a "this should never happen" right before the swear words so they know it wasn't your fault.
Post reply on HN