-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandling.java
More file actions
158 lines (143 loc) · 4.29 KB
/
Copy pathExceptionHandling.java
File metadata and controls
158 lines (143 loc) · 4.29 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.Scanner;
public class ExceptionHandling {
// public static void main(String[] args) {
// int a,b,c;
// Scanner sc=new Scanner(System.in);
// System.out.println("enter a,b values");
// a=sc.nextInt();
// b=sc.nextInt();
// try{
// c=a/b;
// System.out.println("Result: "+c);
// }
// catch(ArithmeticException e){
// System.out.println("denominator should not be zero,try again");
// }
// System.out.println("bye");
// }
// // NESTED CATCH BLOCKS//
// public static void main(String[] args) {
// int A[]={10,20,30,40,0};
// try{
// int c=A[0]/A[1];
// System.out.println("Result: "+c);
// System.out.println(A[5]);
// }
// catch(ArithmeticException e){
// System.out.println("deniminator should not be zero,try again");
// }
// catch(ArrayIndexOutOfBoundsException e){
// System.out.println("Index is invalid");
// }
// System.out.println("bye");
// }
// NESTED TRY BLOCKS//
// public static void main(String[] args) {
// int A[]={10,20,30,40,0};
// try{
// int c=A[0]/A[4];
// System.out.println("Result: "+c);
// try{
// System.out.println(A[5]);
// }
// catch(ArrayIndexOutOfBoundsException e){
// System.out.println("Index is invalid");
// }
// }
// catch(ArithmeticException e){
// System.out.println("deniminator should not be zero,try again");
// }
// System.out.println("bye");
// }
// CHECKED AND UNCHECKED EXCEPTIONS//
// static void fun1(){
// try{
// System.out.println(10/0);
// }
// catch(Exception e){
// System.out.println(e.getMessage());
// e.printStackTrace();
// }
// }
// static void fun2(){
// fun1();
// }
// static void fun3(){
// fun2();
// }
// public static void main(String[] args) {
// fun3();
// }
// THROW AND THROWS //
// static int meth1(){
// return 10/0;
// }
// static void meth2(){
// meth1();
// }
// static void meth3(){
// meth2();
// }
// public static void main(String[] args) {
// try{
// meth3();
// }
// catch(Exception e){
// System.out.println(e);
// }
// }
// DEMO 1//
// static int area(int l,int b) throws Exception
// {
// if(l<0||b<0)
// throw new Exception();
// return l*b;
// }
// static void meth1()throws Exception{
// System.out.println(area(5,-10));
// }
// public static void main(String[] args) //throws Exception//
// {
// try{
// meth1();}
// catch(Exception e){
// System.out.println(e);
// }
// }
// DEMO 2//
// class NegativeDimensionException extends Exception{
// public String toString(){
// return "Dimensios of a rectangle cannot be negative";
// }
// }
// public class ExceptionHandling {
// static int area(int l,int b) throws NegativeDimensionException
// {
// if(l<0||b<0)
// throw new NegativeDimensionException();
// return l*b;
// }
// static void meth1()throws NegativeDimensionException{
// System.out.println(area(5,-10));
// }
// public static void main(String[] args) //throws Exception//
// {
// try{
// meth1();}
// catch(NegativeDimensionException e){
// System.out.println(e);
// }
// }
// FINALLY BLOCK//
public static void main(String[] args) throws Exception{
try{
System.out.println(10/0);
}
catch(ArithmeticException e){
System.out.println(e);
}
finally{
System.out.println("final msg");
}
}
}