Checked vs Unchecked Example
This program demonstrates difference between checked and unchecked exceptions.
Problem Statement
Write a Java program that shows checked exception (IOException) and unchecked exception (ArithmeticException).
Source Code
| 1 | import java.io.*; |
| 2 | public class CheckedVsUncheckedExample { |
| 3 | public static void main(String[] args) { |
| 4 | System.out.println("Eduinq Checked vs Unchecked Example"); |
| 5 | try { |
| 6 | throw new IOException("Eduinq Checked Exception"); |
| 7 | } catch(IOException e) { |
| 8 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 9 | } |
| 10 | try { |
| 11 | int result=10/0; |
| 12 | } catch(ArithmeticException e) { |
| 13 | System.out.println("Eduinq Exception: "+e.getMessage()); |
| 14 | } |
| 15 | } |
| 16 | } |
Program Output
Eduinq Checked vs Unchecked Example Eduinq Exception: Eduinq Checked Exception Eduinq Exception: / by zero
Explanation
Checked exceptions must be declared or handled explicitly. - Unchecked exceptions occur at runtime without declaration. - Program demonstrates both types clearly. - This design improves understanding of exception categories. - In enterprise systems, distinguishing checked vs unchecked ensures proper error handling strategy. Conclusion: Shows difference between checked and unchecked exceptions.