彻底搞清c++中异常机制

首先看看c++中内置的异常类exception

#include<iostream>
#include<string>
#include<stdexcept>
using namespace std;

double diva( double a,double b)
{
 if(b==0)
 {
  throw exception();
 
 }

   return a/b;

}


void main()
{  try{
 
 /*cout<<diva(8,3)<<endl;
 cout<<diva(8,0)<<endl;*/
 double re=diva(8,3);cout<<re<<endl;
 double re1=diva(8,0);
 
 cout<<re1<<endl;
   }catch(exception e)
    {cout<<"exception!"<<endl;
   }
 
}

在MSDN中可以查到c++异常基类定义如下:

class exception {
public:
    exception() throw();
    exception(const exception& rhs) throw();
    exception& operator=(const exception& rhs) throw();
    virtual ~exception() throw();
    virtual const char *what() const throw();
    };

 

当然,用户也可以自定义自己的异常类

#include<exception> 
#include<iostream> 
using namespace std; 
 
//customized exception class 'myException' 
class myException:public exception 

public: 
  const char* what()const throw()//#1  
   { 
        return "ERROR! Don't divide a number by integer zero.\n"; 
  }     
}; 
void check(int y)

     if(y==0) throw myException(); 

//entry of the application 
int main() 

   int x=100,y=0; 
    try 
    { 
        check(y); 
        cout<<x/y; 
    } 
    catch(myException& me) 
    { 
        cout<<me.what(); 
    } 
    system("pause"); 

 

    return 0; 
}

又如





#include<exception> 
#include<iostream> 
using namespace std; 
 
//customized exception class 'myException' 
class myException:public exception 

public: 
  const char* what()const throw() 
  { 
        return "ERROR! Don't divide a number by integer zero.\n"; 
  }     
}; 
void check(int y)throw(int) //any type of exception is permitted 

    /*if(y==0) throw myException();*/
 if(y==0) throw 'x';

void myUnexpected() 

     cout<<"Unexpected exception caught!\n"; 
    system("pause"); 
     exit(-1); 

void myTerminate() 

     cout<<"myTerminate exception caught!\n"; 
    system("pause"); 
     exit(-1); 

//entry of the application 
int main() 

    unexpected_handler oldHandler=set_unexpected(myUnexpected);
 //terminate_handler preHandler=set_terminate(myTerminate);//当catch中没有匹配的类型就调用,最后调用
    int x=100,y=0; 
   try 
   { 
        check(y); 
       cout<<x/y; 
    } 
    catch(int &e) //##1 no catch sentence matches the throw type 
   { 
        cout<<"catch int exception"<<endl; 
    } 
       /*         ##2 if add this part, any type which's not handler before will
                       be caught */
  //catch(myException &e)
  //  {
  //cout<<e.what()<<endl;
  //   }
 
    system("pause"); 
   return 0; 


 

所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate函数的声明
unexpected_handler set_unexpected (unexpected_handler f) throw();
设置新的,返回旧的

● 说明
所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate#include <iostream> 
#include <exception> 
using namespace std; 
void myunexpected () { 
    cerr << "unexpected called\n"; 
    throw 0;     // throws int (in exception-specification) 

void myfunction () throw (int,char) { 
    throw 'x';   // throws char (not in exception-specification) 
 //throw 3;

int main (void) { 
    set_unexpected (myunexpected); 
    try { 
        myfunction(); 
    } 
    catch (int) { 
        cerr << "caught int\n"; 
    }
  catch (char) { 
        cerr << "caught char\n"; 
    } 
    catch (...) { 
        cerr << "caught other exception (non-compliant compiler?)\n"; 
    } 
    return 0; 

#include <iostream> 
#include <exception> 
using namespace std; 
void myunexpected () { 
    cerr << "unexpected called\n"; 
    throw 0;     // throws int (in exception-specification) 

void myfunction () throw (int,char) { 
    throw 'x';   // throws char (not in exception-specification) 
 //throw 3;

int main (void) { 
    set_unexpected (myunexpected); 
    try { 
        myfunction(); 
    } 
    catch (int) { 
        cerr << "caught int\n"; 
    }
  catch (char) { 
        cerr << "caught char\n"; 
    } 
    catch (...) { 
        cerr << "caught other exception (non-compliant compiler?)\n"; 
    } 
    return 0; 

#include <iostream> 
#include <exception> 
using namespace std; 
void myunexpected () { 
    cerr << "unexpected called\n"; 
    throw 0;     // throws int (in exception-specification) 

void myfunction () throw (int,char) { 
    throw 'x';   // throws char (not in exception-specification) 
 //throw 3;

int main (void) { 
    set_unexpected (myunexpected); 
    try { 
        myfunction(); 
    } 
    catch (int) { 
        cerr << "caught int\n"; 
    }
  catch (char) { 
        cerr << "caught char\n"; 
    } 
    catch (...) { 
        cerr << "caught other exception (non-compliant compiler?)\n"; 
    } 
    return 0; 

所谓的unexpected_handler,指的是函数。
如果某个函数出现异常,而该异常未被列到异常列表,则unexpected_handler被系统自动调用。

该函数可以调用 terminate 或者 cstdlib::exit 或者 cstdlib::abort 中止程序的执行。
也可以把异常再次抛出,或者抛出别的异常。
如果抛出的异常不在异常列表,而且列表里有 bad_exception,那么系统会代替它抛出 bad_exception。
如果列表里没有 bad_exception,那么系统自动调用 terminate 终止程序的运行。

默认的 unexpected handler 调用 terminate

而terminate_handler,是没有找到对应的类型,就调用
#include <iostream> 
#include <exception> 
using namespace std; 
void myunexpected () { 
    cerr << "unexpected called\n"; 
    throw 0;     // throws int (in exception-specification) 

void myfunction () throw (int,char) { 
    throw 'x';   // throws char (not in exception-specification) 
 //throw 3;

int main (void) { 
    set_unexpected (myunexpected); 
    try { 
        myfunction(); 
    } 
    catch (int) { 
        cerr << "caught int\n"; 
    }
  /*catch (char) { 
        cerr << "caught char\n"; 
    }  */
    catch (...) { 
        cerr << "caught other exception (non-compliant compiler?)\n"; 
    } 
    return 0; 

又如

例1:自定义一个继承自excepton的异常类myException

C++标准中,定义在<stdexcept>中的任何异常类都派生自exception Class,本例也只是简单地由exception继承,在try段抛出一个异常并捕捉。代码如下:

  1. /*++ test.cpp
  2. version:1.0
  3. decript:define a exception class named myException
  4.         derived from base class exception
  5.         which is declared in <exception>
  6. created:2011-08-14
  7. author: winterlost
  8. --*/ 
  9. #include<exception> 
  10. #include<iostream> 
  11. using namespace std; 
  12.  
  13. //customized exception class 'myException' 
  14. class myException:public exception 
  15. public
  16.     myException():exception("ERROR! Don't divide a number by integer zero.\n"
  17.     { 
  18.     } 
  19. }; 
  20. //entry of the application 
  21. int main() 
  22.     int x=100,y=0; 
  23.     try 
  24.     { 
  25.         if(y==0) throw myException(); 
  26.         else cout<<x/y; 
  27.     } 
  28.     catch(myException& me) 
  29.     { 
  30.         cout<<me.what(); 
  31.     } 
  32.     system("pause"); 
  33.     return 0; 
/*++ test.cpp
version:1.0
decript:define a exception class named myException
		derived from base class exception
		which is declared in <exception>
created:2011-08-14
author:	winterlost
--*/
#include<exception>
#include<iostream>
using namespace std;

//customized exception class 'myException'
class myException:public exception
{
public:
	myException():exception("ERROR! Don't divide a number by integer zero.\n")
	{
	}
};
//entry of the application
int main()
{
	int x=100,y=0;
	try
	{
		if(y==0) throw myException();
		else cout<<x/y;
	}
	catch(myException& me)
	{
		cout<<me.what();
	}
	system("pause");
	return 0;
}

结果如下:
ERROR! Don't divide a number by integer zero.
请按任意键继续. . .                                                  

显然,异常被捕捉到了。此处需要说明的是,VC对异常处理类exception进行了扩展,本例之所以能够使用exception("ERROR!....")的初始化方法正出于这样的原因,C++标准是不允许这样做的。

与此同时,VC又没有遵循标准,有力地支持terminate和unexpected,它只保留了语法,却在编译运行时不提供支持。为了结合terminate和unexpected更加深入了解C++的异常处理,下面的例子采用Dev cpp IDE实现。

例2:依照C++标准实现自定义异常类myException并将throw语句封装到函数check()中

涉及到的更改正如标题所述,(1)重写基类的what()函数,返回错误信息;(2)将throw myException()封装到check()函数中;(3)允许check()函数抛出myException类型的异常。代码如下:

  1. /*++ test.cpp
  2. version:1.1
  3. decript:define a exception class named myException
  4.         according to C++ standard,
  5.         derived from base class exception
  6.         which is declared in <exception>
  7.         !also,encapusulate throw into a function
  8. created:2011-08-14
  9. author: winterlost
  10. --*/ 
  11. #include<exception> 
  12. #include<iostream> 
  13. using namespace std; 
  14.  
  15. //customized exception class 'myException' 
  16. class myException:public exception 
  17. public
  18.    const char* what()constthrow()//#1 
  19.    { 
  20.         return "ERROR! Don't divide a number by integer zero.\n"
  21.    }     
  22. }; 
  23. void check(int y)throw(myException)//#2 
  24.      if(y==0) throw myException(); 
  25. //entry of the application 
  26. int main() 
  27.     int x=100,y=0; 
  28.     try 
  29.     { 
  30.         check(y); 
  31.         cout<<x/y; 
  32.     } 
  33.     catch(myException& me) 
  34.     { 
  35.         cout<<me.what(); 
  36.     } 
  37.     system("pause"); 
  38.     return 0; 
/*++ test.cpp
version:1.1
decript:define a exception class named myException 
        according to C++ standard,
		derived from base class exception
		which is declared in <exception>
		!also,encapusulate throw into a function
created:2011-08-14
author:	winterlost
--*/
#include<exception>
#include<iostream>
using namespace std;

//customized exception class 'myException'
class myException:public exception
{
public:
   const char* what()const throw()//#1 
   {
        return "ERROR! Don't divide a number by integer zero.\n";
   }    
};
void check(int y) throw(myException)//#2
{
     if(y==0) throw myException();
}
//entry of the application
int main()
{
	int x=100,y=0;
	try
	{
		check(y);
		cout<<x/y;
	}
	catch(myException& me)
	{
		cout<<me.what();
	}
	system("pause");
	return 0;
}

结果与例1完全相同。需说明的是,紧跟check()后的throw列表表明允许该函数抛出的异常类型。这里不得不产生疑问,如果抛出了一个不被允许的异常类型将怎样?

例3:抛出unexpected异常

check函数体之后的throw列表,规定了允许抛出的异常类型,一旦违背,就将触发unexpected。可以把unexpected看作系统自动调用的CALLBACK函数,不同的是,也可以手工触发它的执行。本例的情况属于前者。代码如下:

  1. /*++ test.cpp
  2. version:1.3
  3. decript:define an unexpected excption handler,
  4.         set it by using set_unexpected,
  5.         modify the throw list of function check
  6. created:2011-08-14
  7. author: winterlost
  8. --*/ 
  9. #include<exception> 
  10. #include<iostream> 
  11. using namespace std; 
  12.  
  13. //customized exception class 'myException' 
  14. class myException:public exception 
  15. public
  16.    const char* what()constthrow() 
  17.    { 
  18.         return "ERROR! Don't divide a number by integer zero.\n"
  19.    }     
  20. }; 
  21. void check(int y)throw()//#1 only int-type exception is permitted 
  22.      if(y==0) throw myException(); 
  23. void myUnexpected() 
  24.      cout<<"Unexpected exception caught!\n"
  25.      system("pause"); 
  26.      exit(-1); 
  27. //entry of the application 
  28. int main() 
  29.     unexpected_handler oldHandler=set_unexpected(myUnexpected); 
  30.     int x=100,y=0; 
  31.     try 
  32.     { 
  33.         check(y); 
  34.         cout<<x/y; 
  35.     } 
  36.     catch(myException& me) 
  37.     { 
  38.         cout<<me.what(); 
  39.     } 
  40.     system("pause"); 
  41.     return 0; 
/*++ test.cpp
version:1.3
decript:define an unexpected excption handler,
        set it by using set_unexpected,
        modify the throw list of function check
created:2011-08-14
author:	winterlost
--*/
#include<exception>
#include<iostream>
using namespace std;

//customized exception class 'myException'
class myException:public exception
{
public:
   const char* what()const throw()
   {
        return "ERROR! Don't divide a number by integer zero.\n";
   }    
};
void check(int y) throw()//#1 only int-type exception is permitted
{
     if(y==0) throw myException();
}
void myUnexpected()
{
     cout<<"Unexpected exception caught!\n";
     system("pause");
     exit(-1);
}
//entry of the application
int main()
{
    unexpected_handler oldHandler=set_unexpected(myUnexpected);
	int x=100,y=0;
	try
	{
		check(y);
		cout<<x/y;
	}
	catch(myException& me)
	{
		cout<<me.what();
	}
	system("pause");
	return 0;
}

结果如下:

Unexpected exception caught!
请按任意键继续. . .                    

check函数的throw列表为空,即不允许抛出任何类型的异常,然而实际上当异常发生时,系统不能等闲视之,它将调用unexpected处理方法。所以,限定一个函数throw列表为空是值得程序员警醒的事,需要特别留意。如果将#1处的代码修改为throw(int)等也能得到相同的结果。所谓unexpected异常,说白了就是函数体允许抛出异常类型范围之外的异常。如果check函数后面根本没有throw,则表示函数任何类型的异常都被允许。

例4:抛出函数体允许的异常,但没被捕捉到的情况

思考这样一个问题,如果函数check的throw列表中有异常类型myException,而且在y==0时,它的确抛出myException类型的异常,但是没有被catch到,这时会发生什么?

在正式回答这个问题之前,先讨论“没被catch到”的意思。比如,修改例3的代码如下:(##为修改之处)

  1. /*++ test.cpp
  2. version:1.4.1
  3. decript:
  4.         how to understand "exception not caucht"?
  5. created:2011-08-14
  6. author: winterlost
  7. --*/ 
  8. #include<exception> 
  9. #include<iostream> 
  10. using namespace std; 
  11.  
  12. //customized exception class 'myException' 
  13. class myException:public exception 
  14. public
  15.    const char* what()constthrow() 
  16.    { 
  17.         return "ERROR! Don't divide a number by integer zero.\n"
  18.    }     
  19. }; 
  20. void check(int y)//any type of exception is permitted 
  21.      if(y==0) throw myException(); 
  22. void myUnexpected() 
  23.      cout<<"Unexpected exception caught!\n"
  24.      system("pause"); 
  25.      exit(-1); 
  26. //entry of the application 
  27. int main() 
  28.     unexpected_handler oldHandler=set_unexpected(myUnexpected); 
  29.     int x=100,y=0; 
  30.     try 
  31.     { 
  32.         check(y); 
  33.         cout<<x/y; 
  34.     } 
  35.     catch(int &e)//##1 no catch sentence matches the throw type 
  36.     { 
  37.         cout<<e<<endl; 
  38.     } 
  39.     /*               ##2 if add this part, any type which's not handler before will
  40.                         be caught
  41.     catch(...)
  42.     {
  43.                     cout<<"Unkown exception caught!\n";
  44.          }
  45.     */ 
  46.     system("pause"); 
  47.     return 0; 
/*++ test.cpp
version:1.4.1
decript:
        how to understand "exception not caucht"?
created:2011-08-14
author:	winterlost
--*/
#include<exception>
#include<iostream>
using namespace std;

//customized exception class 'myException'
class myException:public exception
{
public:
   const char* what()const throw()
   {
        return "ERROR! Don't divide a number by integer zero.\n";
   }    
};
void check(int y) //any type of exception is permitted
{
     if(y==0) throw myException();
}
void myUnexpected()
{
     cout<<"Unexpected exception caught!\n";
     system("pause");
     exit(-1);
}
//entry of the application
int main()
{
    unexpected_handler oldHandler=set_unexpected(myUnexpected);
	int x=100,y=0;
	try
	{
		check(y);
		cout<<x/y;
	}
	catch(int &e) //##1 no catch sentence matches the throw type
	{
		cout<<e<<endl;
	}
	/*               ##2 if add this part, any type which's not handler before will
	                    be caught
	catch(...)
	{
                    cout<<"Unkown exception caught!\n";
         }
    */
	system("pause");
	return 0;
}

编译运行,程序将会出错,因为check函数抛出的myException异常没有被处理。在缺省情况下,一旦出现抛出异常没被处理的问题,系统将自动调用abort()函数,终止程序允许,在控制台将会看到这样的提示:
This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.

不过可以增加##2部分的代码,catch(...)表示捕捉任何类型的异常。

注意:check函数不被允许的异常类型并不会进入到catch语句的判断中来,因此catch(...)对unexpected exception没有作用。

仍然考虑没有##2部分的情况。正如前面所述,系统将自动调用abort()函数终止程序。实际上,它触发的是terminate,类似于unexpected,仍然可以自定义terminate的处理方法。甚至terminate语法上跟unexpected都十分近似。修改代码为:

  1. /*++ test.cpp
  2. version:1.4.2
  3. decript:
  4.         how to understand "exception not caucht"?
  5. created:2011-08-14
  6. author: winterlost
  7. --*/ 
  8. #include<exception> 
  9. #include<iostream> 
  10. using namespace std; 
  11.  
  12. //customized exception class 'myException' 
  13. class myException:public exception 
  14. public
  15.    const char* what()constthrow() 
  16.    { 
  17.         return "ERROR! Don't divide a number by integer zero.\n"
  18.    }     
  19. }; 
  20. void check(int y)//any type of exception is permitted 
  21.      if(y==0) throw myException(); 
  22. void myUnexpected() 
  23.      cout<<"Unexpected exception caught!\n"
  24.      system("pause"); 
  25.      exit(-1); 
  26. void myTerminate() //##1 set it be the terminate handler 
  27.      cout<<"Unhandler exception!\n"
  28.      system("pause"); 
  29.      exit(-1); 
  30. //entry of the application 
  31. int main() 
  32.     unexpected_handler oldHandler=set_unexpected(myUnexpected); 
  33.     terminate_handler preHandler=set_terminate(myTerminate); 
  34.     int x=100,y=0; 
  35.     try 
  36.     { 
  37.         check(y); 
  38.         cout<<x/y; 
  39.     } 
  40.     catch(int &e)//no catch sentence matches the throw type 
  41.     { 
  42.         cout<<e<<endl; 
  43.     } 
  44.     system("pause"); 
  45.     return 0; 
/*++ test.cpp
version:1.4.2
decript:
        how to understand "exception not caucht"?
created:2011-08-14
author:	winterlost
--*/
#include<exception>
#include<iostream>
using namespace std;

//customized exception class 'myException'
class myException:public exception
{
public:
   const char* what()const throw()
   {
        return "ERROR! Don't divide a number by integer zero.\n";
   }    
};
void check(int y) //any type of exception is permitted
{
     if(y==0) throw myException();
}
void myUnexpected()
{
     cout<<"Unexpected exception caught!\n";
     system("pause");
     exit(-1);
}
void myTerminate() //##1 set it be the terminate handler
{
     cout<<"Unhandler exception!\n";
     system("pause");
     exit(-1);
}
//entry of the application
int main()
{
    unexpected_handler oldHandler=set_unexpected(myUnexpected);
    terminate_handler preHandler=set_terminate(myTerminate);
	int x=100,y=0;
	try
	{
		check(y);
		cout<<x/y;
	}
	catch(int &e) //no catch sentence matches the throw type
	{
		cout<<e<<endl;
	}
	system("pause");
	return 0;
}

结果如下:

Unhandler exception!
请按任意键继续. . .    

结论:C++为异常处理提供了友好的支持。

用户可以自定义异常类型,异常类型并不受到限制,可以是内建数据类型如int,double等,也可以是自定义的类,也可以从C++某个异常类继承下来。例1采用了派生自exception的方法。

除此之外,在定义函数时,可以显式指定函数体抛出的异常类型。隐式情况下,缺省允许函数抛出任何类型的异常。有可以增加throw语句,对异常类型加以限制。特别的是,throw()表示不允许函数抛出任何类型的异常。如果违反了throw列表规定的异常类型,系统将调用unexpected hanlder进行处理,可以自定义unexpected异常处理方法。例2和例3对它们进行了说明。

如果对于函数体throw列表合法的异常被抛出,但是却没有被程序捕捉处理,系统将调用terminate handler进行处理。缺省情况下,只是简单调用abort()函数终止程序,同样可以自定义terminate处理方法。例4对它进行了说明。


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值