zoukankan      html  css  js  c++  java
  • #Exception#Cpp中Exception类的选择

    关于异常的重要性以及用法在本文中暂时不讨论,本文对Exception类的选择进行讨论。

    目录:

    1、Exception类的分类

    2、系统是否会自动抛出异常(讨论了一下,但是没有得出结论)

    3、runtime_error和logic_error的区分

    4、几种常见的Exception

    1、Exception类的分类

     

     异常类的基类为exception,派生出

    1)runtime_error

    包括overflow error,underflow error

    2)logic_error

    包括invalid_argument,length_error,out_of_range

    3)bad_alloc

    比如使用new的时候

    4)bad_cast

    比如使用动态链接的时候

    5)bad_type_id

    比如typeid(*0)

    6)bad_exception

     看到了另外一篇不错的博文,在此分享一下:

    http://blog.csdn.net/zhq651/article/details/8425579

    2、异常的抛出

    是否需要手动抛出异常,系统是否会自动抛出异常。

        try
        {
            c = new int[100000000000000000000000];
            fs.open("test.txt");
            if(fs.fail()){
                cout << "open failed"<<endl;
            }
            cout << "end of try_block"<<endl;
            
        }
        catch(exception &e)
        {
            cout<<"Exception:"<<e.what()<<endl;
            cout<<"Exception type:"<<typeid(e).name()<<endl;
        }

    使用new进行分配空间,无法得到满足,于是自动抛出异常

        try
        {
            fs.open("test.txt");
            if(fs.fail()){
                cout << "open failed"<<endl;
            }
            cout << "end of try_block"<<endl;
            
        }
            catch(exception &e)
        {
            cout<<"Exception:"<<e.what()<<endl;
            cout<<"Exception type:"<<typeid(e).name()<<endl;
        }

    不存在test.txt,但是并没有自动抛出异常

    3、如果系统并不是完全自动抛出异常,那么就需要用户选择异常的类型。对于另外几种比较明确,而logic errors and runtime errors比较难区分。

    stackoverflow上有关于这个的大讨论

    https://stackoverflow.com/questions/2924058/confused-about-stdruntime-error-vs-stdlogic-error/6476858#6476858?newreg=7f576920c83943698339b83d8ca1f1bd

    C++0x Standard says (clause 19.2):上是这么描述的

    1) In the error model reflected in these classes (i.e. the exception types), errors are divided into two broad categories: logic errors and runtime errors.

    2) The distinguishing characteristic of logic errors is that they are due to errors in the internal logic of the program. In theory, they are preventable.

    逻辑错误

    3) By contrast, runtime errors are due to events beyond the scope of the program. They cannot be easily predicted in advance.

    程序的作用范围造成的( the "scope" is defined as the library code's expectations, not your program's design)

    在Cpp标准上也有这样的描述:

    logic_error : "The class logic_error defines the type of objects thrown as exceptions to report errors presumably detectable before the program executes, such as violations of logical preconditions or class invariants." 

    在程序执行前,去对一个逻辑上的先决条件进行判断

    runtime_error : "The class runtime_error defines the type of objects thrown as exceptions to report errors presumably detectable only when the program executes." 

    在程序执行的过程中,报出错误。

    比如,runtime_error的overflow_error是在程序进行计算(运行)的时候发现的。

    而,logic_error的out_of_range,以vector为例,虽然也是程序执行过程中发现的,但是并没有去取vector中真实数据,而是先检查了这个“取操作”是否可行。

    The user could verify that the file exists, run the program, and suddenly learn that the file has been deleted. That's why an I/O problem is always a runtime_error. Stateful problems are runtime_errors, even if the caller could have checked, since another thread could cause the problem.

    logic_error is when the arguments to a function are wrong. It is only for things which could have been caught earlier with stronger type-checking.

    So, "missing file" is a runtime_error, but "improperly formatted filename" is a logic_error.

    Technically, a logical error within a function is a logic_error too, but if you're smart enough to test for it inside your own function, you should be smart enough to prevent it in the first place.

    4、几种常见的Exception

    除法运算中除数为0;runtime_error

    在只读文件系统中以写模式打开一个文件流;logic_error/runtime_error在下面有具体的讨论

    数组a的容量为5,向 a[5] 中写入数据; logic_error

    用 new 申请内存失败;bad_alloc

    将A指针类型转换为B指针类型失败;bad_cast

    将 1234567 这个整数存到一个 short int 类型的变量中(在32位C++编译器中编译)runtime_error

    文件不存在或者文件不可读/写 属于runtime_errors,

    文件名错误属于logic_error 

    The user could verify that the file exists, run the program, and suddenly learn that the file has been deleted. That's why an I/O problem is always a runtime_error. Stateful problems are runtime_errors, even if the caller could have checked, since another thread could cause the problem.

    logic_error is when the arguments to a function are wrong. It is only for things which could have been caught earlier with stronger type-checking.

    So, "missing file" is a runtime_error, but "improperly formatted filename" is a logic_error.

    Technically, a logical error within a function is a logic_error too, but if you're smart enough to test for it inside your own function, you should be smart enough to prevent it in the first place.

  • 相关阅读:
    sublime中node测试环境
    常用的win dos命令
    html area图片热点
    Git常用指令整理(Git Cheat Sheet)
    Java研发技术学习路线
    编程+工具基础教程|网站整理
    廖雪峰 Git 教程 + Git-Cheat-Sheet 学习总结
    现成的HTML5框架
    记录下自己学习的点滴-开始写博客
    linux查看日志文件
  • 原文地址:https://www.cnblogs.com/wuqi/p/4704312.html
Copyright © 2011-2022 走看看