6 ms·
My favorite part is the Java project that has an exception class called ThisShouldNeverHappenException [1]. Only in Java would someone create an exception class
by BinaryIdiot 11y 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 :)
[1] https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46f9fe7ea3c36fc071256e023234/src/com/googlecode/propidle/util/ThisShouldNeverHappenException.java https://github.com/TheProjecter/propidle/blob/f0d5320e2a3d46...
- stinos 11y agoNot 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.
- Kristine1975 11y agoIsn't that what assertions are for?
- Karunamon 11y agoAssertions 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.
- amatix 11y agoin Python, running with `-O`, from .pyo files, or equivalent settings in your webserver will all disable assertions. surprise!
- maggit 11y agoTo 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".
- pionar 11y agoIn C#, I tend to use the built-in InvalidOperationException class - https://msdn.microsoft.com/en-us/library/system.invalidoperationexception%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/en-us/library/system.invalidopera...
- haddr 11y agoI don't think it is a programmer error. Many times when you program against some less reliable API or doing some network stuff the variety of exceptional situations can be really overwhelming. Such exceptions can be some sort of sink to evaluate later whether you still need to improve the code to handle some very specific corner cases.
- yxhuvud 11y agoRight. The best ones are the ones that tell not only that something is wrong, but explicitly says what should be fixed.
- viach 11y agoExceptions 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.
- fahadkhan 11y agoThere's http://docs.oracle.com/javase/6/docs/api/java/lang/IllegalStateException.html http://docs.oracle.com/javase/6/docs/api/java/lang/IllegalSt... that could be used in this case.
- jkot 11y agoWhy man has nipples if he does not breast feed?
- hiimnate 11y agoI could ask you the very same question.
- rodh 11y agoYep. There's What A Terrible Failure exception in the Android codebase (i.e. "throw wtf"). http://developer.android.com/reference/android/util/Log.html#wtf%28java.lang.String,%20java.lang.String%29 http://developer.android.com/reference/android/util/Log.html... My code made the Android OS throw this once. Not a terribly useful error to resolve. Edit: My mistake. I misremembered. It's a logging function. I should have actually read the link I provided.
- vbezhenar 11y agoI would use AssertionError exception for that.