-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandling.java
More file actions
56 lines (45 loc) · 2.04 KB
/
Copy pathExceptionHandling.java
File metadata and controls
56 lines (45 loc) · 2.04 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package exceptions;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
* Exception handling: try / catch / finally, multiple catch blocks, common exceptions
*/
public class ExceptionHandling {
private static final Logger logger = Logger.getLogger(ExceptionHandling.class.getName());
public static void main() {
// Example 1: Division by zero | Arithmetic error (ArithmeticException)
try {
int result = 10 / 0;
logger.log(Level.INFO, "Result: {0}", result); // Never executes
} catch (ArithmeticException e) {
logger.log(Level.SEVERE, "Arithmetic error: {0}", e.getMessage());
}
// Example 2: Out of bounds access | Array error (ArrayIndexOutOfBoundsException)
int[] numbers = {1, 2, 3};
try {
logger.log(Level.INFO, "{0}", numbers[5]);
} catch (ArrayIndexOutOfBoundsException e) {
logger.log(Level.SEVERE, "Index out of bounds: {0}", e.getMessage());
}
// Example 3: Multiple catch blocks + finally | Generic error (Exception)
String text = null;
try {
logger.log(Level.INFO, "{0}", text.length()); // NullPointerException
} catch (NullPointerException _) {
logger.log(Level.SEVERE, "Error: null reference.");
} catch (Exception e) {
logger.log(Level.SEVERE, "Generic error: {0}", e.getMessage());
} finally {
// Finally will always execute, whether an error occurs or not
logger.info("Finally block: release any resources here.");
}
// Example 4: Invalid conversion (NumberFormatException) | Number format error
try {
int parsedNumber = Integer.parseInt("abc"); // NumberFormatException
logger.log(Level.INFO, "Parsed number: {0}", parsedNumber);
} catch (NumberFormatException _) {
logger.info("Could not convert the text to a number.");
}
logger.info("The program continues normally after handling all errors.");
}
}