-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathTryCatchFinally.java
51 lines (38 loc) · 1.85 KB
/
TryCatchFinally.java
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
public class TryCatchFinally {
//exception handling using try-catch-finally keywords
public static void main(String[] args) {
System.out.println("********************CASE 1:********************");
tryCatch();
System.out.println("********************CASE 2:********************");
tryCatchFinally();
System.out.println("********************CASE 3:********************");
tryCatchFinallyNoException();
}
public static void tryCatch(){
try{ //code that can throw exceptions will be enclosed in the try block
int i=10/0; //will throw an arithmetic exception as we are attemting division by zero on integer data type
}catch(ArithmeticException arith_exc){ //catching the instance of the exception using catch keyword
System.out.println(arith_exc + " caught in catch block"); //handling the exception
}
}
public static void tryCatchFinally(){
try{
int i=10/0; //will throw an arithmetic exception as we are attemting division by zero on integer data type
}catch(ArithmeticException arith_exc){ //catching the instance of the exception using catch keyword
System.out.println(arith_exc + " caught in catch block"); //handling the exception
}finally{
//finally block will be executed regardless whether an exception is thrown or not
System.out.println("We are in the finally block" );
}
}
public static void tryCatchFinallyNoException(){
try{
int i=10/5; //will not throw an arithmetic exception
}catch(ArithmeticException arith_exc){ //no exception would be caught by catch
System.out.println(arith_exc + " caught in catch block");
}finally{
//finally block will be executed regardless whether an exception is thrown or not
System.out.println("We are in the finally block" );
}
}
}