Search code examples
javaexceptionchecked-exceptions

Comparing checked and unchecked exceptions (Performance, Benchmarks) in Java


Are there scenarios where you could argue that when an exception is created that both a checked exception and an unchecked exception can be used? Then the speed and performance can be measured against each other


Solution

  • From the perfomance point of view, building stack trace for exception is anyway the longest operation when an exception is generated (details).

    Checked and unchecked exceptions make difference only at compile time. The Java compiler forces you to either catch checked exceptions or declare them in the method signature. It was supposed to improve program safety, but the majority opinion seems to be that it's not worth the design problems it creates.

    There is even Lomboks "magical" @SneakyThrows annotation to overcome this compile-time check. On the JVM (class file) level, all exceptions, checked or not, can be thrown regardless of the throws clause of your methods, which is why this works.