I would like to contribute a list that groups by situation, which is probably more meaningful than grouping by packages or fields of programming.
Unexpected exceptions
These are exceptions that should ideally never be thrown in production. You should fix them instead of catching them and stfu.
when you test newly written code (and sometimes unexpectedly seen by users)
They should never happen again once you see them.
- AssertionError
- congratulations, you are writing good code
- you should be happy that you made this error, because less (errors) is more (bugs)
- I hope you won't end up finding out that your code is correct and you accidentally inverted your assertion
- NullPointerException
- where are your
@NotNull
s?
- did you check before using the return value from
Map.get()
?
- ArrayIndexOutOfBoundsException
- do you check before using
List.get()
?
- I hope you know well enough that Java isn't JavaScript, arrays are fixed-sized and you can't just
array[array.length] = newElement
- ClassCastException
- too much generics is bad for your brain cells; consider moving to golang!
- IllegalArgumentException
- this could sometimes mean "read the fishing docs"
And less likely to see,
- IllegalMonitorStateException
- yes, having to
synchronized
sucks, but it's like this in most languages
- CloneNotSupportedException
- hey, just don't use clone(). aren't copy constructors cool?
And then you get more NullPointerException.
And then... Still NullPointerException? That @NotNull
annotation is rubbish!
when you test newly written code for the 100th time
Exceptions that happen due to race conditions or rare probability. You should buy a lottery if you see them the first 10 times you run your code.
- ConcurrentModificationException
- ?your
synchronized
is where
- IllegalStateException
- StackOverflowError (not Exception)
- have you tried tail recursion?
during compilation, deployment, etc.
They usually happen when you messed up the dependencies, used wrong versions of libraries, etc.
- LinkageError
- NoClassDefFoundError
- java.lang.XxxNotFoundException, java.lang.NoSuchXxxException (classes, methods, etc.)
when you are too lazy
and when you are using @lombok.SneakyThrows
or equivalent
- RuntimeException
- ? extends RuntimeException
Expected exceptions
If they are not caught, this probably means you are too lazy as well. You can't prevent them from throwing; you just have to catch them.
high likeliness
These exceptions have high likeliness to happen, and should always be handled specifically (i.e. you should actually handle them instead of just outputting the error)
- NumberFormatException
- I never understand why this exception extends RuntimeException.
medium likeliness
These exceptions sometimes happen due to invalid user input (but you should really validate them, so I classify these as "unexpected exceptions"), and sometimes happen due to system constraints that might not be reproducible when (non-stress) testing.
- IOException and other java.io.XxxException
- SecurityException
- StackOverflowException
- unfortunately, going to StackOverflow most likely won't fix your StackOverflowExceptions, because you will get more overflown stacks as you are looking for something to Ctrl-C and Ctrl-V
- StackUnderflowException
- no, StackOverflow is still not going to help a lot
- OutOfMemoryError
- I hope it's not a memory leak