019-001-008
Finally Block and Resource Cleanup
Hard
Problem Description
Finally Block and Resource Cleanup
In this problem, you will implement a Resource class and verify that each method behaves correctly.
Learning Objective: Understand finally block behavior and how to reliably release resources
Specifications
Implement a Resource class:
open(): outputs "Resource opened" (no exception)use(boolean shouldFail):- If
shouldFailistrue, throwsRuntimeException("Operation failed") - If
shouldFailisfalse, outputs "Resource used" (no exception)
- If
close(): outputs "Resource closed" (no exception)
Verification Points
open()→ no exceptionclose()→ no exceptionuse(false)→ no exceptionuse(true)→ throwsRuntimeException("Operation failed")
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up