3 ms·
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 InternalErrorEx
by stinos 11y ago
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.
- 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.