-
Notifications
You must be signed in to change notification settings - Fork 0
/
que1.cpp
46 lines (38 loc) · 977 Bytes
/
que1.cpp
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
#include <iostream>
#include <exception>
using namespace std;
// Declare a user-defined exception
class NegativeValException : public exception { // LINE-1
public:
virtual const char* what() const throw() {
return "Negative value";
}
};
// Declare a user-defined exception
class ZeroValException : public exception { // LINE-2
public:
virtual const char* what() const throw() {
return "Zero";
}
};
int main() {
int i;
cin >> i;
try {
if (i < 0)
// Throw the exception object
throw NegativeValException(); // LINE-3
else if (i == 0)
// Throw the exception object
throw ZeroValException();
else
cout << i << " is accepted";
}
catch (NegativeValException e) {
cout << e.what() << endl;
}
catch (ZeroValException e) {
cout << e.what() << endl;
}
return 0;
}