Skip to main content

Practice ยท 1 of 3

Close Order & Suppression

Model in-memory resources inside Solution: - static class Res implements AutoCloseable with a StringBuilder log passed in and a name; close() appends name + ":closed" to the log. - static String run(boolean failSecond, StringBuilder log): opens new Res("a", log), then new Res("b", log) in ONE try-with-resources statement (body appends "work" to the log); when failSecond is true the body throws IllegalStateException AFTER the first resource was opened. Return log.toString(). Correct behavior: - happy path: work then b:closed then a:closed (reverse order) - failure: a:closed b:closed STILL both closed, exception propagates

Difficulty: intermediate

Back to lesson: Practice: Exception Architecture Lab