在《C++异常入门》一节中,我们讲到了 C++ 异常处理的流程,具体为:
异常必须显式地抛出,才能被检测和捕获到;如果没有显式的抛出,即使有异常也检测不到。
在 C++ 中,我们使用 throw 关键字来显式地抛出异常,它的用法为:
exceptionData 是“异常数据”的意思,它可以包含任意的信息,完全有程序员决定。exceptionData 可以是 int、float、bool 等基本类型,也可以是指针、数组、字符串、结构体、类等聚合类型,请看下面的例子:
- char str[] = "http://www.cdsy.xyz";
- char *pstr = str;
- class Base{};
- Base obj;
- throw 100; //int 类型
- throw str; //数组类型
- throw pstr; //指针类型
- throw obj; //对象类型
C/C++ 规定,数组一旦定义后,它的长度就不能改变了;换句话说,数组容量不能动态地增大或者减小。这样的数组称为静态数组(Static array)。静态数组有时候会给编码代码不便,我们可以通过自定义的 Array 类来实现动态数组(Dynamic array)。所谓动态数组,是指数组容量能够在使用的过程中随时增大或减小。
下面这段代码虽然有点长,但它是一个典型的使用异常的场景,请大家耐心阅读。
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- //自定义的异常类型
- class OutOfRange{
- public:
- OutOfRange(): m_flag(1){ };
- OutOfRange(int len, int index): m_len(len), m_index(index), m_flag(2){ }
- public:
- void what() const; //获取具体的错误信息
- private:
- int m_flag; //不同的flag表示不同的错误
- int m_len; //当前数组的长度
- int m_index; //当前使用的数组下标
- };
- void OutOfRange::what() const {
- if(m_flag == 1){
- cout<<"Error: empty array, no elements to pop."<<endl;
- }else if(m_flag == 2){
- cout<<"Error: out of range( array length "<<m_len<<", access index "<<m_index<<" )"<<endl;
- }else{
- cout<<"Unknown exception."<<endl;
- }
- }
- //实现动态数组
- class Array{
- public:
- Array();
- ~Array(){ free(m_p); };
- public:
- int operator[](int i) const; //获取数组元素
- int push(int ele); //在末尾插入数组元素
- int pop(); //在末尾删除数组元素
- int length() const{ return m_len; }; //获取数组长度
- private:
- int m_len; //数组长度
- int m_capacity; //当前的内存能容纳多少个元素
- int *m_p; //内存指针
- private:
- static const int m_stepSize = 50; //每次扩容的步长
- };
- Array::Array(){
- m_p = (int*)malloc( sizeof(int) * m_stepSize );
- m_capacity = m_stepSize;
- m_len = 0;
- }
- int Array::operator[](int index) const {
- if( index<0 || index>=m_len ){ //判断是否越界
- throw OutOfRange(m_len, index); //抛出异常(创建一个匿名对象)
- }
- return *(m_p + index);
- }
- int Array::push(int ele){
- if(m_len >= m_capacity){ //如果容量不足就扩容
- m_capacity += m_stepSize;
- m_p = (int*)realloc( m_p, sizeof(int) * m_capacity ); //扩容
- }
- *(m_p + m_len) = ele;
- m_len++;
- return m_len-1;
- }
- int Array::pop(){
- if(m_len == 0){
- throw OutOfRange(); //抛出异常(创建一个匿名对象)
- }
- m_len--;
- return *(m_p + m_len);
- }
- //打印数组元素
- void printArray(Array &arr){
- int len = arr.length();
- //判断数组是否为空
- if(len == 0){
- cout<<"Empty array! No elements to print."<<endl;
- return;
- }
- for(int i=0; i<len; i++){
- if(i == len-1){
- cout<<arr[i]<<endl;
- }else{
- cout<<arr[i]<<", ";
- }
- }
- }
- int main(){
- Array nums;
- //向数组中添加十个元素
- for(int i=0; i<10; i++){
- nums.push(i);
- }
- printArray(nums);
- //尝试访问第20个元素
- try{
- cout<<nums[20]<<endl;
- }catch(OutOfRange &e){
- e.what();
- }
- //尝试弹出20个元素
- try{
- for(int i=0; i<20; i++){
- nums.pop();
- }
- }catch(OutOfRange &e){
- e.what();
- }
- printArray(nums);
- return 0;
- }
运行结果:
Array 类实现了动态数组,它的主要思路是:在创建对象时预先分配出一定长度的内存(通过 malloc() 分配),内存不够用时就再扩展内存(通过 realloc() 重新分配)。Array 数组只能在尾部一个一个地插入(通过 push() 插入)或删除(通过 pop() 删除)元素。
我们通过重载过的[ ]运算符来访问数组元素,如果下标过小或过大,就会抛出异常(第53行代码);在抛出异常的同时,我们还记录了当前数组的长度和要访问的下标。
在使用 pop() 删除数组元素时,如果当前数组为空,也会抛出错误。
throw 关键字除了可以用在函数体中抛出异常,还可以用在函数头和函数体之间,指明当前函数能够抛出的异常类型,这称为异常规范(Exception specification),有些教程也称为异常指示符或异常列表。请看下面的例子:
这条语句声明了一个名为 func 的函数,它的返回值类型为 double,有一个 char 类型的参数,并且只能抛出 int 类型的异常。如果抛出其他类型的异常,try 将无法捕获,只能终止程序。
如果函数会抛出多种类型的异常,那么可以用逗号隔开:
如果函数不会抛出任何异常,那么( )中什么也不写:
如此,func() 函数就不能抛出任何类型的异常了,即使抛出了,try 也检测不到。
C++ 规定,派生类虚函数的异常规范必须与基类虚函数的异常规范一样严格,或者更严格。只有这样,当通过基类指针(或者引用)调用派生类虚函数时,才能保证不违背基类成员函数的异常规范。请看下面的例子:
- class Base{
- public:
- virtual int fun1(int) throw();
- virtual int fun2(int) throw(int);
- virtual string fun3() throw(int, string);
- };
- class Derived:public Base{
- public:
- int fun1(int) throw(int); //错!异常规范不如 throw() 严格
- int fun2(int) throw(int); //对!有相同的异常规范
- string fun3() throw(string); //对!异常规范比 throw(int,string) 更严格
- }
C++ 规定,异常规范在函数声明和函数定义中必须同时指明,并且要严格保持一致,不能更加严格或者更加宽松。
请看下面的几组函数:
- //错!定义中有异常规范,声明中没有
- void func1();
- void func1() throw(int) { }
- //错!定义和声明中的异常规范不一致
- void func2() throw(int);
- void func2() throw(int, bool) { }
- //对!定义和声明中的异常规范严格一致
- void func3() throw(float, char*);
- void func3() throw(float, char*) { }
异常规范的初衷是好的,它希望让程序员看到函数的定义或声明后,立马就知道该函数会抛出什么类型的异常,这样程序员就可以使用 try-catch 来捕获了。如果没有异常规范,程序员必须阅读函数源码才能知道函数会抛出什么异常。
不过这有时候也不容易做到。例如,func_outer() 函数可能不会引发异常,但它调用了另外一个函数 func_inner(),这个函数可能会引发异常。再如,您编写的函数调用了老式的库函数,此时不会引发异常,但是库更新以后这个函数却引发了异常。总之,异常规范的初衷实现起来有点困难,所以大家达成的一致意见是,最好不要使用异常规范。
异常规范是 C++98 新增的一项功能,但是后来的 C++11 已经将它抛弃了,不再建议使用。
另外,各个编译器对异常规范的支持也不一样,请看下面的代码:
- #include <iostream>
- #include <string>
- #include <exception>
- using namespace std;
- void func()throw(char*, exception){
- throw 100;
- cout<<"[1]This statement will not be executed."<<endl;
- }
- int main(){
- try{
- func();
- }catch(int){
- cout<<"Exception type: int"<<endl;
- }
- return 0;
- }
在 GCC 下,这段代码运行到第 7 行时程序会崩溃。虽然 func() 函数中发生了异常,但是由于 throw 限制了函数只能抛出 char*、exception 类型的异常,所以 try-catch 将捕获不到异常,只能交给系统处理,终止程序。
在 Visual C++ 下,输出结果为Exception type: int,这说明异常被成功捕获了。在 Visual C++ 中使用异常规范虽然没有语法错误,但是也没有任何效果,Visual C++ 会直接忽略异常规范的限制,函数可以抛出任何类型的异常。