forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionExample.java
More file actions
48 lines (42 loc) · 960 Bytes
/
ExceptionExample.java
File metadata and controls
48 lines (42 loc) · 960 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.examplehub.basics;
public class ExceptionExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
/*
* Index 5 out of bounds for length 5
*/
/*
* comment to fixed LGTM
try {
System.out.println(numbers[5]);
} catch (IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}
*/
/*
* ArithmeticException
* Finished
*/
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
System.out.println("ArithmeticException");
} finally {
System.out.println("Finished");
}
/*
* divisor is zero
*/
try {
int result = divide(3, 0);
} catch (ArithmeticException e) {
System.out.println(e.getMessage());
}
}
public static int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("divisor is zero");
}
return a / b;
}
}