Finally for Resource Cleanup
mediumQuestion 218Simulate 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. Catches ArithmeticException and 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, so 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.