← 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
Topics:
Miscellaneous
Code Editor
1
Tab to indent · Ctrl+Enter to run · 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.