-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptionFun2.cpp
More file actions
60 lines (52 loc) · 1.28 KB
/
exceptionFun2.cpp
File metadata and controls
60 lines (52 loc) · 1.28 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
#include <iostream>
#include <stdexcept>
using namespace std;
void processPositive(int num);
void doSomething(int num);
int main() {
int input;
try {
cout << "Enter a number to process!" << endl;
cin >> input;
doSomething(input);
cout << "Yay! main was able to completely process the num!" << endl;
}
catch (const invalid_argument& err) {
cout << "main says there is an error!" << endl;
cout << err.what() << endl;
}
catch (out_of_range& err)
{
cout << "main says number is too big" << endl;
cout << err.what() << endl;
}
return 0;
}
void processPositive(int num) {
cout << "Welcome to the positive integer processor!" << endl;
if (num >= 0 && num <= 100) {
cout << "Good job! You passed in a positive num to processPositive!" << endl;
}
else if(num > 100)
{
throw out_of_range("Number is too big.");
}
else {
throw invalid_argument("Negative number passed in!");
}
}
void doSomething(int num) {
try {
processPositive(num);
cout << "Yay! doSomething could process the num!" << endl;
}
catch (const invalid_argument& err) {
cout << "doSomething says there is a problem!" << endl;
throw;
}
catch (out_of_range& err)
{
cout << "doSomething says number is too big" << endl;
throw;
}
}