//37_21days_Cpp_异常_(1)-txwtech-cc37a_demo.cpp //异常 //传统的处理方法 //现代的处理方法:处理异常 //》---try //>>---catch //>>---throw //>>异常类型 //》》---数字 //》》---字符串 //》》---类对象 //fopen()打开文件,fread()读取文件,fwrite()写入文件-使用方法
//现代的处理方法:处理异常.抛出数字
//现代的处理方法:处理异常.抛出字符串
//现代的处理方法:处理异常.抛出对象
//37_21days_Cpp_异常_(1)-txwtech-cc37a_demo.cpp
//异常
//传统的处理方法
//现代的处理方法:处理异常
//》---try
//>>---catch
//>>---throw
//>>异常类型
//》》---数字
//》》---字符串
//》》---类对象
//fopen()打开文件,fread()读取文件,fwrite()写入文件-使用方法
//#include
//
//using namespace std;
//
//int main()
//{
//
// getchar();
// return 0;
//}
#define _CRT_SECURE_NO_WARNINGS
//传统处理方法202001202008
#include
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");//打开源文件,r读,b二进制方式
if (in_file == NULL)
{
return 1; //异常,执行出错,也可以返回2....
}
out_file = fopen(dest_file,"wb");//w写,b二进制方式
if (out_file == NULL)
{
return 2;//异常,执行出错
}
char rec[256];//做缓存
size_t bytes_in, bytes_out;//记录读写字节数
//bytes_in=fread(rec,1,256,in_file);//读文件,1,一个字节一个字节读,读256字节。
//(bytes_in>0)
while ((bytes_in = fread(rec, 1, 256, in_file)) > 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; //正常结束
}
//现代的处理方法:处理异常.抛出数字
void my_cp2(const char *src_file, const char * dest_file)
{
//声明两个文件指针
FILE *in_file, *out_file;
in_file = fopen(src_file, "rb");//打开源文件,r读,b二进制方式
if (in_file == NULL)
{
//return 1; //异常,执行出错,也可以返回2....
throw 1;//1就是个异常
}
out_file = fopen(dest_file, "wb");//w写,b二进制方式
if (out_file == NULL)
{
//return 2;//异常,执行出错
throw 2;
}
char rec[256];//做缓存
size_t bytes_in, bytes_out;//记录读写字节数
//bytes_in=fread(rec,1,256,in_file);//读文件,1,一个字节一个字节读,读256字节。
//(bytes_in>0)
while ((bytes_in = fread(rec, 1, 256, in_file)) > 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);
//return 0; //正常结束
}
//现代的处理方法:处理异常.抛出字符串
void my_cp3(const char *src_file, const char * dest_file)
{
//声明两个文件指针
FILE *in_file, *out_file;
in_file = fopen(src_file, "rb");//打开源文件,r读,b二进制方式
if (in_file == NULL)
{
//return 1; //异常,执行出错,也可以返回2....
throw "打开源文件时出错";//1就是个异常
}
out_file = fopen(dest_file, "wb");//w写,b二进制方式
if (out_file == NULL)
{
//return 2;//异常,执行出错
throw "打开目标文件时出错";
}
char rec[256];//做缓存
size_t bytes_in, bytes_out;//记录读写字节数
//bytes_in=fread(rec,1,256,in_file);//读文件,1,一个字节一个字节读,读256字节。
//(bytes_in>0)
while ((bytes_in = fread(rec, 1, 256, in_file)) > 0)
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//写文件
if (bytes_in != bytes_out)
{
//return 3;
throw "拷贝文件时出错";
}
}
fclose(in_file);
fclose(out_file);
//return 0; //正常结束
}
//现代的处理方法:处理异常.抛出对象
void my_cp4(const char *src_file, const char * dest_file)
{
//声明两个文件指针
FILE *in_file, *out_file;
in_file = fopen(src_file, "rb");//打开源文件,r读,b二进制方式
if (in_file == NULL)
{
//return 1; //异常,执行出错,也可以返回2....
//throw "打开源文件时出错";//1就是个异常
throw BadSrcFile();
}
out_file = fopen(dest_file, "wb");//w写,b二进制方式
if (out_file == NULL)
{
//return 2;//异常,执行出错
//throw "打开目标文件时出错";
throw BadDestFile();
}
char rec[256];//做缓存
size_t bytes_in, bytes_out;//记录读写字节数
//bytes_in=fread(rec,1,256,in_file);//读文件,1,一个字节一个字节读,读256字节。
//(bytes_in>0)
while ((bytes_in = fread(rec, 1, 256, in_file)) > 0)
{
bytes_out = fwrite(rec, 1, bytes_in, out_file);//写文件
if (bytes_in != bytes_out)
{
//return 3;
//throw "拷贝文件时出错";
throw BadCopy ();
}
}
fclose(in_file);
fclose(out_file);
//return 0; //正常结束
}
int main()
{
int result;
if ((result = my_cp("d:\\temp\\1.txt", "d:\\temp\\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("d:\\temp\\1.txt", "d:\\temp\\33.txt");//此处异常,直接跳到catch,my_cp3与后面的都不执行了
my_cp3("d:\\temp\\1.txt", "d:\\temp\\33.txt");
my_cp4("d:\\temp\\11.txt", "d:\\temp\\33.txt");
}
catch (int e) //因为throw 1,1是数字,所以 定义int/catch异常只会执行一个
{
printf("发生异常:%d\n",e);//异常都放在e里面
}
catch (const char *e)//抓取字符串的异常-catch异常只会执行一个
{
printf("发生异常:%s\n", e);//异常都放在e里面
}
catch (BadSrcFile e)//catch异常只会执行一个
{
printf("bad对象打开源文件时出错\n");
}
catch (BadDestFile e)//catch异常只会执行一个
{
printf("bad对象打开目标文件时出错\n");
}
catch (BadCopy e)//catch异常只会执行一个
{
printf("bad对象发生未知错误\n");
}
catch (...)//3个点表示任何异常,都能抓住
{
printf("发生了未知异常\n");
}
printf("OK!\n");
}