-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
41 lines (38 loc) · 1.09 KB
/
Main.java
File metadata and controls
41 lines (38 loc) · 1.09 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
public class Main{
public static void main(String[] args) {
int a = 5;
int b = 0;
try{
// int c = a/b;
// divide(a, b);
// Math.divideExact(3,0);
//Creating exception purposefully!
String name = "JD";
if(name.equals("JD")){
throw new MyException("Just for fun");
}
}
//Custom exception
catch(MyException e){
System.out.println(e.getMessage());
}
//Handle specific exception
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
//Handles all kind of exceptions!
catch(Exception e){
System.out.println("Normal Exception Event");
}
finally{
System.out.println("This will always execute!");
}
}
static int divide(int a, int b) throws ArithmeticException{
if(b == 0){
//throw error explicitly
throw new ArithmeticException("Please do not divide by zero!");
}
return a/b;
}
}