C++中的try、catch跟throw以及传统处理异常的方法

首先:try跟catch是捕获异常,然后throw则是抛出异常,类似return的作用

传统处理异常方法:判断返回值等,或者判断是否为空,然后做出响应的处理

新型处理异常:try catch()捕获异常,只在块内捕获,比如有什么突然断网之类的异常情况,或者赋值一个变量值的分母为0,没有捕获则会程序崩溃,加了则会继续运行
throw,抛出异常
catch捕获的类型须跟throw一样,如果throw出int 型,那catch写法就应是catch(int e){printf(“发生异常”,e)},如果是字符串类型catch(const char * e){printf("%s",e)},还可抛出一个类
catch可有多个,
catch(…)表示取得所有异常,最好前面的catch写好,然后加…抓漏网之鱼,try第一行语句发生异常,后面的不会执行了,直接去catch

传统处理异常的方法
判断返回值,或者判断是否为空,然后做出相应的处理。
下面展示一个简单的文件复制example
大家通过复制代码去编译可以更容易理解一些。

// 
#include <stdio.h>

int my_cp(const char *src_file,const char *dest_file)
{
    FILE *in_file,*out_file;
    in_file = fopen(src_file,"rb");
    if(in_file==NULL)
    {
        return 1;//返回1出错
    }
    out_file = fopen(dest_file,"wb");
    if(out_file==NULL)
    {
        return 2;//返回2出错
    }
    
    char rec[256];//定义一个缓冲区数组用来接收临时数据
    size_t bytes_in, bytes_out;

    while((bytes_in = fread(rec,1,256,in_file))>0)//判断1文件夹是否有内容
    {
		printf("正在复制\n");
        bytes_out = fwrite(rec,1,bytes_in,out_file);//赋值到2文件夹
        if(bytes_in != bytes_out)//判断写入与写出的是否一样
        {
            return 3;//返回3出错
        }
    }
    fclose(in_file);
    fclose(out_file);
    return 0;//返回0正常
}
int main()
{
    int result;
    if(result = my_cp("d:\\temp\\1.txt","d:\\temp\\2.txt")!=0)
    //提前在d盘创建temp目录以及1.txt跟2.txt,然后上面代码单独更改其中一个txt文件名看报错
    {
        switch(result)
        {
            case 1:
                printf("打开源文件时出错\n");
                break;
            case 2:
                printf("打开目标文件时出错\n");
                break;
            case 3:
                printf("拷贝文件时出错\n");
                break;
            default:
                printf("发生未知错误\n");
        }
    }

    printf("OK\n");
}

传统的判断异常方法通过判断返回值,从而判断具体的错误类型,
而try、catch则可以自动捕获异常,throw就可以抛出异常,从上面程序修改的程序如下所示,这个throw抛出的是数字:可以自己复制代码运行一下修改代码查看错误的类型

#include <stdio.h>
void my_cp(const char *src_file,const char *dest_file)
{
    FILE *in_file,*out_file;
    in_file = fopen(src_file,"rb");
    if(in_file==NULL)
    {
        throw 1;//返回1出错
    }
    out_file = fopen(dest_file,"wb");
    if(out_file==NULL)
    {
        throw 2;//返回2出错
    }
    
    char rec[256];//定义一个缓冲区数组用来接收临时数据
    size_t bytes_in, bytes_out;

    while((bytes_in = fread(rec,1,256,in_file))>0)//判断1文件夹是否有内容
    {
		printf("正在复制\n");
        bytes_out = fwrite(rec,1,bytes_in,out_file);//赋值到2文件夹
        if(bytes_in != bytes_out)//判断写入与写出的是否一样
        {
            throw 3;//返回3出错
        }
    }
    fclose(in_file);
    fclose(out_file);
    //return 0;//返回0正常
}
void main()
{
	try
	{
		my_cp("d:\\temp\\11.txt","d:\\temp\\2.txt");
	}
	catch(int e)
	{
		printf("发生异常,异常码为:%d\n",e);
	}

    printf("OK\n");
}

下面的则是抛出字符串的例程代码:
这个throw抛出的是字符串
注意,一个程序可以有多个catch捕获不同种类的异常,这里改的只是throw跟catch里的值,这两类型必须一样

// An highlighted block
#include <stdio.h>
void my_cp(const char *src_file,const char *dest_file)
{
    FILE *in_file,*out_file;
    in_file = fopen(src_file,"rb");
    if(in_file==NULL)
    {
        throw "打开源文件出错";
    }
    out_file = fopen(dest_file,"wb");
    if(out_file==NULL)
    {
        throw "打开目标文件出错";
    }
    
    char rec[256];//定义一个缓冲区数组用来接收临时数据
    size_t bytes_in, bytes_out;

    while((bytes_in = fread(rec,1,256,in_file))>0)//判断1文件夹是否有内容
    {
		printf("正在复制\n");
        bytes_out = fwrite(rec,1,bytes_in,out_file);//赋值到2文件夹
        if(bytes_in != bytes_out)//判断写入与写出的是否一样
        {
           throw "文件复制出错";
        }
    }
    fclose(in_file);
    fclose(out_file);
    //return 0;//返回0正常
}
void main()
{
	try
	{
		my_cp("d:\\temp\\11.txt","d:\\temp\\2.txt");
	}
	catch(const  char * e)
	{
		printf("发生异常,异常为:%s\n",e);
	}

    printf("OK\n");
}

下面例子则是抛出一个类对象,catch(…)的话就是捕获所有除上面判断外其他的异常

// An highlighted block
#include <stdio.h>
class BadSrcFile{};
class BadDestFile{};
class BadCopy{};
void my_cp(const char *src_file,const char *dest_file)
{
    FILE *in_file,*out_file;
    in_file = fopen(src_file,"rb");
    if(in_file==NULL)
    {
        throw BadSrcFile();
    }
    out_file = fopen(dest_file,"wb");
    if(out_file==NULL)
    {
        throw BadDestFile();
    }
    
    char rec[256];//定义一个缓冲区数组用来接收临时数据
    size_t bytes_in, bytes_out;

    while((bytes_in = fread(rec,1,256,in_file))>0)//判断1文件夹是否有内容
    {
		printf("正在复制\n");
        bytes_out = fwrite(rec,1,bytes_in,out_file);//赋值到2文件夹
        if(bytes_in != bytes_out)//判断写入与写出的是否一样
        {
           throw BadCopy();
        }
    }
    fclose(in_file);
    fclose(out_file);
   
}
void main()
{
	try
	{
		my_cp("d:\\temp\\11.txt","d:\\temp\\2.txt");
	}
	catch(BadSrcFile e)
	{
		printf("发生异常,打开源文件出错\n");
	}
	catch(BadDestFile e)
	{
		printf("发生异常,打开目标文件出错\n");
	}
	catch(BadCopy e)
	{
		printf("发生异常,复制文件出错\n");
	}
	catch(...)
	{
		printf("发生未知异常");
	}
    printf("OK\n");
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值