博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++之异常处理
阅读量:7287 次
发布时间:2019-06-30

本文共 1849 字,大约阅读时间需要 6 分钟。

 C++ Code 
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
 
/*
    名称:C++ 异常处理
    作者:Michael Joessy
    日期:2017-06-06
    知识:
    异常:程序在运行期出现的错误
    异常处理:对有可能发生异常的地方做出预见性的安排
    你如果不处理,就会传给操作系统暴力处理,如崩溃。
    关键字:try...catch...  throw
            尝试  捕获      抛出异常
    基本思想:主逻辑与异常逻辑处理分离
    try与catch是一对多的关系
*/
#include
 <iostream>
#include
 <string>
using
 
namespace
 std;
void
 fun1()
{
    
throw
 
1
;
}
char
 getChar(
const
 string& strA, 
const
 
int
 nIndex)
{
    
if
 (nIndex > strA.size())
    {
        
throw
 string(
"invalid index!"
);
    }
    
return
 strA[nIndex];
}
class
 Excepction
{
public
:
    Excepction(){}
    
virtual
 ~Excepction(){}
    
virtual
 
void
 printException()
    {
        cout << 
"Exception->printException"
 << endl;
    }
};
class
 indexException : 
public
 Excepction
{
public
:
    
virtual
 
void
 printException()
    {
        cout << 
"Exception -- 下标越界!"
 << endl;
    }
};
void
 test()
{
    
throw
 indexException();
};
int
 main(
void
)
{
    
/************************************************************************/
    
/* 常见异常
        数组下标越界
        除数为0
        内存不足
    /* 异常处理与多态的关系
        Exception接口类,子类如下:
        HardwareErr
        SizeErr
        MemoryErr
        NetWorkErr
        抛出子类异常时,可以用父类来捕获子类的异常!
    /************************************************************************/
    
try
    {
#if
 
0
        fun1();
#endif
#if
 
0
        string str(
"Michael Joessy"
);
        
char
 ch = getChar(str, 
100
);
        cout << ch << endl;
#endif
#if
 
0
   
        test();
#endif
        
    }
    
catch
 (
int
)
    {
        cout << 
"Exception:int"
 << endl;
    }
    
catch
(
double
)
    {
        cout << 
"Exception:double"
 << endl;
    }
    
catch
(string& str)
    {
        cout << str << endl;
    }
    
catch
(Excepction& e)      
//基类捕获所有子类的异常信息 Go
    {
        e.printException();
    }
    
catch
(indexException& e)
    {
        e.printException();
    }
    
catch
(...)
    {
    }
    cin.get();
    
return
 
0
;
}

转载地址:http://expjm.baihongyu.com/

你可能感兴趣的文章
phalapi-入门篇4(国际化高可用和自动生成文档)
查看>>
xcode报错集锦_1
查看>>
hadoop-mapreduce分析
查看>>
多线程学习(4)wait/notify
查看>>
OSChina 周五乱弹——让人伤心的事
查看>>
Golang配置
查看>>
android下拉刷新
查看>>
linux 中route命令的使用
查看>>
ArrayList既然继承自AbstractList抽象类,而AbstractList已经实现了List接口,那么ArrayList类为何还要再实现List接口呢?...
查看>>
CentOS安装Redis
查看>>
在iOS上实现一个简单的日历控件
查看>>
Android——Type mismatch类型转换错误的根源
查看>>
4.Utm详细实现-用户资源管理
查看>>
CentOS7.3安装Python3.6
查看>>
怎么才能用ABBYY FineReader提高工作效率
查看>>
STORM 落入MONGO速度优化
查看>>
python:守护进程deamon
查看>>
coding项目怎样和其他人共享
查看>>
Android wifi 设置相关
查看>>
vue中一个关于input元素的小坑
查看>>