-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP50_TryCatchFinallyDivideByZero.java
More file actions
39 lines (34 loc) · 1.6 KB
/
Copy pathP50_TryCatchFinallyDivideByZero.java
File metadata and controls
39 lines (34 loc) · 1.6 KB
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
31
32
33
34
35
36
37
38
39
package programs;
/**
* ============================================================
* PROGRAM 50: Try-Catch-Finally and Multi-Catch Handling
* ============================================================
* Problem: WAP to demonstrate try, multi-catch, and finally block
* handling `ArithmeticException`, `ArrayIndexOutOfBoundsException`,
* and `NumberFormatException`.
* ============================================================
*/
public class P50_TryCatchFinallyDivideByZero {
public static void safeExecute(String numStr, int divisor, int arrayIndex) {
System.out.printf("%nExecuting with numStr='%s', divisor=%d, idx=%d:%n", numStr, divisor, arrayIndex);
int[] buffer = {10, 20, 30};
try {
int parsed = Integer.parseInt(numStr);
int result = parsed / divisor;
int item = buffer[arrayIndex];
System.out.printf(" ✓ Success: Result=%d, Item=%d%n", result, item);
} catch (ArithmeticException | NumberFormatException e) {
System.out.println(" ❌ Caught Math/Parse Error: " + e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(" ❌ Caught Array Bounds Error: " + e.getMessage());
} finally {
System.out.println(" [Finally] Cleanup routine completed unconditionally.");
}
}
public static void main(String[] args) {
safeExecute("100", 2, 1); // Success
safeExecute("100", 0, 1); // Divide by zero
safeExecute("abc", 5, 0); // Parse error
safeExecute("500", 10, 10); // Array index error
}
}