Java MCQ: What does the ‘finally’ block do in Java?
a) Executes code after the try-catch block
b) Skips exception handling
c) Defines a constant
d) Terminates the program
Answer:
a) Executes code after the try-catch block
Explanation:
The finally
block in Java is used to execute code after a try-catch
block, regardless of whether an exception was thrown or caught. It is typically used for resource cleanup, such as closing files or releasing network connections.
Understanding the finally
block is important for ensuring that critical code runs even if an exception occurs, helping to maintain resource integrity in Java applications.
Reference links:
https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html