c++ 异常抛出 throw

throw介绍:
throw是用来抛出异常的。与之对应的处理异常的关键字还有try catch。throw一般是要被放在try语块中的用来抛出异常,而在对应的catch语块中进行相应异常的处理。
用举个例子吧:
========================================
#include <stdio.h>
#include <string.h>

char* _strcpy(char *d, const char *s)
{
  try
  {
   char *temp;
   if (NULL == d || NULL == s)
    throw "Invalid argument(s)";   //这里抛出一个字符串异常(char*)类型的
   temp = d;
   while(*d++ = *s++);
   return temp;
  }
  catch(char* e)        //在这里接住这个char*类型的异常,e就是指向异常对象:"Invalid argument(s)"
  {
   printf("%s", e);
  }
} 

int main(int argc, char* argv[])
{
 char a[32];
 _strcpy(a, NULL);
 return 0;
}
======================================
运行结果:
Invalid argument(s)
======================================

上面这种抛出C风格的字符串的情况比较少见,一般都是抛出一个异常类型的对象。再把上面的_strcpy函数改一下,用string类做为一个异常对象:
char* _strcpy(char *d, const char *s)
{
  try
  {
   char *temp;
   if (NULL == d || NULL == s)
    throw string("Invalid argument(s)");
   temp = d;
   while(*d++ = *s++)
   {}
   return temp;
  }
  catch(string &e)
  {
   printf("%s\n", e.c_str());
  }
} 
进一步说明:

 
 

代码均用g++(3.4.2版本)编译,微软的cl编译器会出现不同结果,估计是用非标准C++的结果。 首先定义一个简单的异常类 =============================== #include<string> using namespace std; class myException { public: myException(string err):_msg(err){} ~myException(){} string dump(){return _msg;} private: string _msg; }; =============================== 定义三个函数分别能抛出myException,string异常,string异常,和不会抛出异常 在C++PRIMER中: void f1(int whitch) throw(myException, string); //保证不会抛出myException, string之外的异常 void f2() throw(string); //保证不会抛出string之外的异常 void f3() throw(); //保证不会抛出任何异常 见下面的代码: ================================================= #include"exception.h"

void f1(int whitch) throw(myException, string); void f2() throw(string); void f3() throw();

int main() { try {    f3(); } catch(string& e) {    cout<<e<<endl; } catch(myException& e) {    cout<<e.dump()<<endl; } catch(...) {    cout<<"other err"<<endl; } return 0; } void f1(int whitch) throw(myException, string) { if( 0 == whitch ) {    throw myException("this myException from f1");

} else {    throw string("this stringException from f1"); } } void f2() throw(string) { f(1); throw string("this stringException from f2"); } void f3() throw() { f2(); } ===========================================================

运行结果: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

分析:f2中的string异常,在f2内部没有处理,抛出到f3中,而f3中也没有处理,需要继续向上抛,但是f3保证不抛出异常。到此产生运行时错误。系统调用terminate函数,而terminate会调用about终止程序。 为验证以上分析结果,去掉f3后面的throw,编译运行,得到结果:this stringException from f2

============================================================= 在f2中调用f1抛出一个string的异常: ================================= void f2() throw(string) { f1(1); throw string("this stringException from f2"); } ================================= 运行结果: this stringException from f1 ================================ 在f2中调用f1抛出一个myException类型的异常: ================================= void f2() throw(string) { f1(0); throw string("this stringException from f2"); } ================================= 运行结果: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

分析:由于f1中抛出的是一个myException的异常,而f2只能够抛出string型的异常。所以这个myException没有地方处理,故系调用terminate函数终止程序。 =================================

结论: 1、函数后面的throw()用于指定该函数可以抛出的异常,括号中指定异常类型,若为空则说明不允许抛出异常。若不限定异常类型,则不要加throw(). 2、没有在函数声明中声明的异常,不能够从函数体中抛出,必须在本函数中捕获并处理。 3、函数中没有被捕获的异常会继续向上抛,直到main函数中,若还没有被处理,则系统调用terminate终止进程。


                
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值