-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC34_tryCatchBlock.java
More file actions
30 lines (23 loc) · 972 Bytes
/
C34_tryCatchBlock.java
File metadata and controls
30 lines (23 loc) · 972 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
27
28
29
30
public class C33_ErrorAndException34_tryCatchBlock {
public static void main(String[] args) {
System.out.println("Program Started...");
// Example 1: Handling divide-by-zero error
try {
int a = 10;
int b = 0;
int result = a / b; // ❌ This will throw ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide a number by zero.");
System.out.println("Exception Message: " + e.getMessage());
}
// Example 2: Handling array index error
try {
int[] arr = {1, 2, 3};
System.out.println(arr[5]); // ❌ ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Error: Invalid array index accessed!");
}
System.out.println("Program Ended Normally...");
}
}