相关内容:
异常提供了一种转移程序控制权的方式。C++
异常处理涉及到三个关键字:try
、catch
、throw
。
throw
: 当问题出现时,程序会抛出一个异常。这是通过使用throw
关键字来完成的。catch
: 在您想要处理问题的地方,通过异常处理程序捕获异常。catch
关键字用于捕获异常。try
:try
块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个catch
块。
如果有一个块抛出一个异常,捕获异常的方法会使用try
和catch
关键字。try
块中放置可能抛出异常的代码,try
块中的代码被称为保护代码。用try/catch
语句的语法如下所示:1
2
3
4
5
6
7
8
9
10
11
12
13try
{
// 保护代码
}catch( ExceptionName e1 )
{
// catch 块
}catch( ExceptionName e2 )
{
// catch 块
}catch( ExceptionName eN )
{
// catch 块
}- 如果
try
块在不同的情境下会抛出不同的异常,这个时候可以尝试罗列多个catch
语句,用于捕获不同类型的异常。 - 抛出异常:您可以使用
throw
语句在代码块中的任何地方抛出异常。throw
语句的操作数可以是任意的表达式,表达式的结果的类型决定了抛出的异常的类型。 - 捕获异常:
catch
块跟在try
块后面,用于捕获异常。您可以指定想要捕捉的异常类型,这是由catch
关键字后的括号内的异常声明决定的。异常捕获的基本原理如下图:程序相关内容:
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
class BadSrcFile {};
class BadDestFile {};
class BadCopy{};
//传统方法
int my_cp(const char *src_file, const char *dest_file) // 实现功能:从一个文件中拷贝到另一文件;
{
FILE *in_file, *out_file; // 定义了两个文件指针
in_file = fopen(src_file, "rb"); //以二进制读的形式打开文件src_file;
if (in_file == NULL)
{
return 1;//如果读到的文件为空,或者没有能够打开文件则返回1;
}
out_file = fopen(dest_file, "wb"); //以二进制写的形式打开文件src_file;
if (out_file == NULL)
{
return 2;
}
char rec[256]; //先定义一个256字节的rec,用来缓冲,先将文件读到rec中,读取文件一次最多读取256字节,然后再从rec写到文件中;
size_t bytes_in, bytes_out;
while ((bytes_in = fread(rec,1,256,in_file)) > 0) //每次读一个字节,读256个,bytes_in为实际读到的字节数,如果最后不足256就读取实际剩余的字节,如果返回为0,说明文件已读完;
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//将读取到的字节写入文件中;
if (bytes_in != bytes_out)
{
return 3;
}
}
fclose(in_file);
fclose(out_file);//关闭文件;
return 0;
}
//使用异常 ,可以不需要返回值;throw 的类型为数字;
void my_cp2(const char *src_file, const char *dest_file) // 实现功能:从一个文件中拷贝到另一文件;
{
FILE *in_file, *out_file; // 定义了两个文件指针
in_file = fopen(src_file, "rb"); //以二进制读的形式打开文件src_file;
if (in_file == NULL)
{
//return 1;//如果读到的文件为空,或者没有能够打开文件则返回1;
throw 1; //使用抛出异常的方式;throw的可以是数字,字符串,也可以是类对象,此处使用数字;
}
out_file = fopen(dest_file, "wb"); //以二进制写的形式打开文件src_file;
if (out_file == NULL)
{
//return 2;
throw 2;
}
char rec[256]; //先定义一个256字节的rec,用来缓冲,先将文件读到rec中,读取文件一次最多读取256字节,然后再从rec写到文件中;
size_t bytes_in, bytes_out;
while ((bytes_in = fread(rec, 1, 256, in_file)) > 0) //每次读一个字节,读256个,bytes_in为实际读到的字节数,如果最后不足256就读取实际剩余的字节,如果返回为0,说明文件已读完;
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//将读取到的字节写入文件中;
if (bytes_in != bytes_out)
{
//return 3;
throw 3;
}
}
fclose(in_file);
fclose(out_file);//关闭文件;
}
//使用异常 ,可以不需要返回值;throw 的类型为字符串;
void my_cp3(const char *src_file, const char *dest_file) // 实现功能:从一个文件中拷贝到另一文件;
{
FILE *in_file, *out_file; // 定义了两个文件指针
in_file = fopen(src_file, "rb"); //以二进制读的形式打开文件src_file;
if (in_file == NULL)
{
//return 1;//如果读到的文件为空,或者没有能够打开文件则返回1;
throw "打开源文件时出错。";
}
out_file = fopen(dest_file, "wb"); //以二进制写的形式打开文件src_file;
if (out_file == NULL)
{
//return 2;
throw "打开目标文件时出错。";
}
char rec[256]; //先定义一个256字节的rec,用来缓冲,先将文件读到rec中,读取文件一次最多读取256字节,然后再从rec写到文件中;
size_t bytes_in, bytes_out;
while ((bytes_in = fread(rec, 1, 256, in_file)) > 0) //每次读一个字节,读256个,bytes_in为实际读到的字节数,如果最后不足256就读取实际剩余的字节,如果返回为0,说明文件已读完;
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//将读取到的字节写入文件中;
if (bytes_in != bytes_out)
{
//return 3;
throw "拷贝时出错。";
}
}
fclose(in_file);
fclose(out_file);//关闭文件;
}
//使用异常 ,可以不需要返回值;throw 的类型为类对象;
void my_cp4(const char *src_file, const char *dest_file) // 实现功能:从一个文件中拷贝到另一文件;
{
FILE *in_file, *out_file; // 定义了两个文件指针
in_file = fopen(src_file, "rb"); //以二进制读的形式打开文件src_file;
if (in_file == NULL)
{
//return 1;//如果读到的文件为空,或者没有能够打开文件则返回1;
throw BadSrcFile();
}
out_file = fopen(dest_file, "wb"); //以二进制写的形式打开文件src_file;
if (out_file == NULL)
{
//return 2;
throw BadDestFile();
}
char rec[256]; //先定义一个256字节的rec,用来缓冲,先将文件读到rec中,读取文件一次最多读取256字节,然后再从rec写到文件中;
size_t bytes_in, bytes_out;
while ((bytes_in = fread(rec, 1, 256, in_file)) > 0) //每次读一个字节,读256个,bytes_in为实际读到的字节数,如果最后不足256就读取实际剩余的字节,如果返回为0,说明文件已读完;
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//将读取到的字节写入文件中;
if (bytes_in != bytes_out)
{
//return 3;
throw BadCopy();
}
}
fclose(in_file);
fclose(out_file);//关闭文件;
}
int main()
{
int result;
if ((result = my_cp("e:\\1.txt", "e:\\2.txt")) != 0)
{
switch (result)
{
case 1:
printf("打开源文件时出错!\n");
break;
case 2:
printf("打开目标文件时出错!\n");
break;
case 3:
printf("拷贝文件时出错!\n");
break;
default:
printf("发生未知错误!\n");
break;
}
}
try //将可能发生异常的代码放在此处的大括号内;
{
my_cp2("e:\\1.txt", "e:\\3.txt");
my_cp3("e:\\1.txt", "e:\\4.txt");
my_cp4("e:\\11.txt", "e:\\5.txt");
}
catch (int e) //对异常进行捕获;
{
printf("发生异常:%d\n", e);//此处的e就是发生异常对应的数字,也就是上面throw出的数字;也可以像上面的case对异常情况进行输出;
}
catch (const char * e)//对异常进行捕获;
{
printf("%s\n", e);
}
catch (BadSrcFile e)
{
printf("发生异常:打开源文件时出错!\n");
}
catch (BadDestFile e)
{
printf("发生异常:打开目标文件时出错!\n");
}
catch (BadCopy e)
{
printf("发生异常:拷贝时出错!\n");
}
catch (...) //对所有的异常进行捕获,可以不要前面的catch的情况;
{
printf("发生了未知异常。");
}
printf("OK!\n");
return 0;
}代码执行结果:
(注意:得到执行结果的前提是:e:\\1.txt
文件路径存在,e:\\11.txt
文件路径不存在。)参考链接:
C++ 异常处理
C++异常处理入门(try和catch)
C++ 异常机制分析