-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCheckedVsUncheckedException.java
More file actions
26 lines (23 loc) · 1012 Bytes
/
CheckedVsUncheckedException.java
File metadata and controls
26 lines (23 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package ExceptionHandling;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class CheckedVsUncheckedException {
public static void main(String[] args) {
// Unchecked exception (RuntimeException)
try {
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Throws ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Unchecked exception caught: " + e.getMessage());
}
// Checked exception (must be handled or declared)
try (FileReader fr = new FileReader("nonexistent.txt")) {
// This line will not be reached as the file does not exist.
System.out.println("File opened.");
} catch (FileNotFoundException e) {
System.out.println("Checked exception caught: " + e.getMessage());
} catch (java.io.IOException e) {
System.out.println("I/O Exception: " + e.getMessage());
}
}
}