forked from rahulXbarnwal/JavaTutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnExceptions.java
More file actions
40 lines (34 loc) · 1.14 KB
/
LearnExceptions.java
File metadata and controls
40 lines (34 loc) · 1.14 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
package exceptions;
// Types of errors
// - Syntax error, Logical error, Runtime error
// Program will crash during run time errors
// so we will handle those exceptions
// Exception handling is a way to handle the runtime errors so that
// normal flow of application can be maintained
// Exception is an event that disrupts the normal flow of program,
// It is an object which is thrown at runtime
public class LearnExceptions {
public static void main(String[] args) {
int numerators[] = {10, 200, 30, 40};
int denominators[] = {1, 2, 0, 4};
for(int i = 0; i < 10; i++) {
try {
System.out.println(divide(numerators[i], denominators[i]));
} catch (Exception e) {
System.out.println(e);
}
}
System.out.println("Good job :)");
}
public static int divide(int a, int b) {
try {
return a/b;
} catch (ArithmeticException e) {
System.out.println("Arithmetic exception :(");
return -1;
} catch (Exception e) {
System.out.println(e);
return -1;
}
}
}