-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_3_18_chapter5.cpp
More file actions
64 lines (60 loc) · 1.33 KB
/
cpp_3_18_chapter5.cpp
File metadata and controls
64 lines (60 loc) · 1.33 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
#include <iostream>
#include <stdexcept>
using std::cin;
using std::cout;
using std::endl;
void test1()
{ // 测试switch执行中,在某case之前定义变量
int nA = 3;
switch (nA)
{
case 1:
int nB;
nB = 2;
break;
case 2:
{
int nC;
nC = 4;
break;
}
default:
cout << nB << endl;
// cout << nC << endl; ERROR
break;
}
}
void exercise5_23()
{ // 输入两个数字 第二个时0的话异常处理
int i, j;
std::runtime_error err{"error 1"};
std::runtime_error err2{"error 2"};
while (cout << "input two int" << endl &&
cin >> i >> j)
{
try
{
if (j == 0)
throw std::runtime_error("error 3");
}
catch (std::runtime_error err3) // err3 相当于传入参数的变量名
{
cout << err2.what() << endl; // 输出err2
cout << err3.what() << endl; // 输出 throw 的赋值
cout << "try again?(y/n)" << endl;
char cF;
cin >> cF;
if (cF == 'y')
continue;
else
break;
}
cout << "answer is" << static_cast<double>(i) / j << endl;
}
}
int main(int argc, char const *argv[])
{
// test1();
exercise5_23();
return 0;
}