forked from tejaspundpal/Java-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp8_Exception_Handling.java
More file actions
122 lines (114 loc) · 3.57 KB
/
Copy pathExp8_Exception_Handling.java
File metadata and controls
122 lines (114 loc) · 3.57 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
package college_programs;
import java.util.Scanner;
class MyException extends Exception {
MyException(String str) {
super(str);
}
}
class Operation {
int a;
int b;
Operation() {
a = 0;
b = 0;
}
int addition(int a, int b)// throws TooLongAdditionException
{
try {
if ((a + b) > 1000) {
throw new MyException("TooLongAdditionException");
} else {
return a + b;
}
} catch (MyException e) {
System.out.println("Caught Exception...");
System.out.println(e.getMessage());
}
return 0;
}
int subtraction(int a, int b) // throws NegativeAnswerException
{
try {
if ((a - b) < 0) {
throw new MyException("NegativeAnswerException");
} else {
return a - b;
}
} catch (MyException e) {
System.out.println("Caught Exception...");
System.out.println(e.getMessage());
}
return 0;
}
int multiply(int a, int b) // throws tooLongMultiplicationException
{
try {
if ((a * b) > 5000) {
throw new MyException("tooLongMultiplicationException");
} else {
return a * b;
}
} catch (MyException e) {
System.out.println("Caught Exception...");
System.out.println(e.getMessage());
}
return 0;
}
int divide(int a, int b)// throws DividedByZeroException
{
try {
if (b == 0) {
throw new MyException("DividedByZeroException");
}
else if(b > a){
throw new MyException("DenominatorIsGreaterThanNumeratorException");
}
else {
return a / b;
}
} catch (MyException e) {
System.out.println("Caught Exception...");
System.out.println(e.getMessage());
}
return 0;
}
}
public class Exp8_Exception_Handling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Operation op = new Operation();
System.out.println("Enter 1st no : ");
int a = sc.nextInt();
System.out.println("Enter 2nd no : ");
int b = sc.nextInt();
boolean check = true;
while (check) {
int choice;
System.out.println("1)Addition\n2)Subtraction\n3)Multiplication\n4)Division\n5)Exit\n");
System.out.println("Enter your choice : ");
choice = sc.nextInt();
switch (choice) {
case 1:
int ans = op.addition(a, b);
System.out.println("Addition : " + ans);
break;
case 2:
int ans1 = op.subtraction(a, b);
System.out.println("Subtraction : " + ans1);
break;
case 3:
int ans2 = op.multiply(a, b);
System.out.println("Multiplication : " + ans2);
break;
case 4:
int ans3 = op.divide(a, b);
System.out.println("Division : " + ans3);
break;
case 5:
check = false;
default:
System.out.println("Invalid choice....!!");
}
}
}
}