Skip to main content

Practice ยท 3 of 3

Domain Errors That Carry Data

Model payments in Solution: - static class PaymentException extends RuntimeException - static final class InsufficientFundsException extends PaymentException carrying int shortfallCents with a getter. - static void withdraw(int balance, int amount): throws IllegalArgumentException for negative amount; throws InsufficientFundsException with shortfall amount - balance when amount > balance; otherwise completes (no-op). - static int shortfallOf(RuntimeException e): returns ((InsufficientFundsException) e).shortfallCents() when applicable, otherwise -1. The tests catch at both granularities.

Difficulty: intermediate

Back to lesson: Practice: Exception Architecture Lab