Java exception exercises
Uncaught runtime exceptions take down production Java services: NullPointerExceptions triggered by unexpected null values or auto-unboxing, array indexing beyond bounds, and silent exception swallowing.
Trace the stack trace, locate the source of the exception, and apply defensive coding practices.
The exceptions bugs you will meet
- Unboxing a null wrapper object (e.g. Integer to int) throws an immediate NullPointerException.
- Using <= array.length in loop conditions causes ArrayIndexOutOfBoundsException on the final pass.
- Invoking .equals() on a null reference crashes; put string literals on the left side: "expected".equals(var).
- Catching Exception and doing nothing swallows critical errors and makes bugs impossible to trace.
The exercises
Easy
Medium
- Unexpected NullPointerException during sum calculationNull and undefined
- Inconsistent date formatting results in concurrent requestsAsync and race conditions
- Thread hangs unexpectedly after external service callAsync and race conditions
- Unexpected null return instead of expected exceptionLogic
- Cloned list elements exhibit unexpected shared stateLogic
- Unexpected Resource Behavior in Nested Try-With-ResourcesScope and closure
- ArrayStoreException with covariant array assignmentType
- BigDecimal divide without rounding mode throws exceptionLogic
- ExecutorService thread pool prevents JVM shutdownAsync and race conditions
Errors you might be searching for
More JavaScript practice by topic
Questions
What is modern Java's approach to null safety?
Use java.util.Optional for return values that may be absent, Objects.requireNonNullElse for fallbacks, and annotations like @NonNull.