← Back to Miscellaneous
Question 218
Finally for Resource Cleanup
Finally for Resource Cleanup
Simulate opening and closing a database connection. Write a program that: 1. Prints"Connection opened"2. Reads an integer and divides 100 by it, printing"Result: X"3. CatchesArithmeticExceptionand prints"Query failed"4. Always prints"Connection closed"at the end — even if an exception occurred Use try-catch-finally. Print"Connection opened"before the try block. ⚠️ AUTO-INPUT MODE: The input stream will automatically contain'0\n'.
Expected Output:
Connection opened Query failed Connection closed
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Miscellaneous
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.