forked from hengqiali/AwesomeCpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain15.cpp
More file actions
41 lines (35 loc) · 850 Bytes
/
main15.cpp
File metadata and controls
41 lines (35 loc) · 850 Bytes
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
#include <iostream>
#include <exception>
using namespace std;
/****************************************
C++ 异常
*****************************************/
int main()
{
int x;
int y;
cout << "请输入整数:" << endl;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
try //try包含需要异常检查的代码块
{
if(x < y)
throw 0; //抛出异常1
else
throw 1; //抛出异常2
}
catch(int e) //捕获异常 catch(std::exception e) cout << e.what();
{
switch(e) //根据异常类型分别处理
{
case 0:
cout << "x 小于 y" << endl;
break;
case 1:
cout << "x 大于 y" << endl;
break;
}
}
}