Files

1.8 KiB

exceptions

Companion code for the ankurm.com post "Java Exception Handling Deep Dive: Checked vs Unchecked, Suppression, and What Exceptions Actually Cost." Module exceptions in java-core-examples.

All explanation lives in the post; this module holds the runnable evidence and the captured output.

Versions

Component Version
JDK 25.0.4.1+1 (Temurin, LTS)
JMH 1.37
JUnit Jupiter 5.11.0
Hardware 2 vCPU x86-64 VM (timings are indicative, not a leaderboard)

Quickstart

export JDK25_HOME=/path/to/jdk-25
./scripts/run-all.sh        # rebuilds and regenerates everything in output/

What is in here

File Shows Output
SuppressedDemo try/finally loses the original exception; try-with-resources suppresses the close() failure instead 01
ChainingDemo Caused by / ... N more, and wrapping without the cause 02
CheckedVsUncheckedDemo which types are checked; UncheckedIOException in a lambda 03
CustomHierarchyDemo an unchecked base type with an error code; the 4-argument Throwable constructor 04
FinallyTrapDemo return inside finally discards the exception 05
src/broken/*.java four sources that deliberately fail to compile; javac's real message is captured 06-09
OmitStackTraceDemo HotSpot replaces a hot implicit exception with one that has no stack trace 10
ExceptionCostBenchmark JMH: normal vs no-stack-trace vs preallocated vs return code, at three stack depths 11
DeepTraceDemo stack traces are capped at MaxJavaStackTraceDepth = 1024 frames 12, 14
ExceptionBehaviourTest 8 assertions behind the claims above 13

The JMH run is one fork, 3 warmup and 5 measurement iterations of 1 s; re-running moves the numbers by tens of percent but not the ordering.