3 ms·
I used to use try/catch with an empty catch block in Java all the time. The exception could never in fact occur, but the compiler required me to either add a ca
by gweinberg 13y ago
I used to use try/catch with an empty catch block in Java all the time. The exception could never in fact occur, but the compiler required me to either add a catch block or declare that my function raises an exception, when it fact it doesn't.
Yes, I did include a "this never happens" comment. That is my favorite comment, but only if it is true.
- wheaties 13y agoAh, it was people like you who did this even when, as you said it should never happen, that caused me many a late night... 'cause it did and that empty catch block hid it. Don't leave empty catch blocks. At least put in some form of logging of the exception, just in case.
- Mindless2112 13y agoThe most common code I write in C# is in switches on enums: default: throw new InvalidOperationException("Should never happen!");
- alecdbrooks 13y agoMy thought would be to add an assertion that always fails inside the catch block, but this probably only makes sense if you use assertions already, as they have to be turned on explicitly in Java.
- buerkle 13y agoYears ago I found the source of my troubles which took longer than it should have because Tomcat had an empty catch around InterruptedException with a "this will never happen" comment.
- gst 13y agoWhy don't you just throw a runtime exception inside the catch block?