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 shouldFail is true, throws RuntimeException("Operation failed")
    • If shouldFail is false, outputs "Resource used" (no exception)
  • close(): outputs "Resource closed" (no exception)

Verification Points

  • open() → no exception
  • close() → no exception
  • use(false) → no exception
  • use(true) → throws RuntimeException("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