Using Finally Block
This program demonstrates the use of finally block.
Problem Statement
Write a Java program that uses try-catch-finally to ensure cleanup operations.
Source Code
| 1 | public class FinallyBlockExample { |
| 2 | public static void main(String[] args) { |
| 3 | System.out.println("Eduinq Finally Block Example"); |
| 4 | try { |
| 5 | int a=10, b=0; |
| 6 | int c=a/b; |
| 7 | System.out.println("Eduinq Result: "+c); |
| 8 | } catch(ArithmeticException e) { |
| 9 | System.out.println("Eduinq Exception: Division by zero"); |
| 10 | } finally { |
| 11 | System.out.println("Eduinq Finally Block Executed"); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
Eduinq Finally Block Example Eduinq Exception: Division by zero Eduinq Finally Block Executed
Explanation
The finally block executes regardless of exception occurrence. - It is used for cleanup operations like closing files or releasing resources. - Even if an exception occurs, finally ensures critical code runs. - This design improves reliability and resource management. - In enterprise systems, finally blocks guarantee consistent cleanup. Conclusion: Shows use of finally block in exception handling.