019-003-011
Try-With-Resources and Multiple Exceptions
Hard
Problem Description
Try-With-Resources and Multiple Exceptions
In this problem: You will create a SimpleResource class implementing AutoCloseable and handle multiple exceptions with try-with-resources.
Learning Objective: Understand automatic resource management with try-with-resources and exception handling
Overview
The try-with-resources statement (Java 7+) automatically closes resources implementing AutoCloseable. Exceptions can occur both during resource use and during close().
Specifications
SimpleResourceimplementsAutoCloseable:namefielduse(): outputs"Using: {name}". Throws RuntimeException if name is"bad"close(): outputs"Closing: {name}"
- In Main:
- Use a normal resource
SimpleResource("goodResource") - Use a failing resource
SimpleResource("bad")
- Use a normal resource
- Verify automatic closing with try-with-resources
Output Format
Using: goodResource
Closing: goodResource
Using: bad
Closing: bad
Error: Resource failed: bad
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