abort 异常终止程序
异常捕获机制(try-catch)
(本章节中例子都是用 VS2005 编译调试的)
调用 abort() 函数来终止发现异常的程序. abort() 函数将直接终止程序而不是首先返回到主函数(在 VC 下的入口函数 main[控制台] 或 WinMain[窗体程序])中
例子:
1 #include <iostream> 2 #include <cstdlib> 3 4 double hmean(double a,double b) 5 { 6 if(a == -b) 7 { 8 std::cout<<"untenable arguments to hmean()"<<std::endl; 9 std::abort(); //发现错误,终止程序. 10 } 11 return 2.0*a*b/(a+b); 12 } 13 void main() 14 { 15 double x,y,z; 16 std::cout<<"Enter two number: \n"; 17 while(std::cin>>x>>y) 18 { 19 z = hmean(x,y); 20 std::cout<<"Barmonic mean of "<<x<<" and "<<y<<" is "<<z<<std::endl; 21 std::cout<<"Enter next set of numbers < q to quit >:"; 22 } 23 std::cout<<"Bye!"<<std::endl; 24 system("pause"); 25 }
当输入为 12 -12 时候输出结果( 编译器为 VS2005 ):
[返回目录]
[工作原理][声明原型][工作流程][异常机制与函数返回机制区别][优点][注意问题][将对象作为异常类型][异常规范][exception类][bad_alloc类]
工作原理:
声明原型:
try{
... //try block
throw typeX_argX;//抛出异常
... //try block
}
catch(type1 arg1){
... //catch block
}
catch(type2 arg2){
... //catch block
}
...
catch(typeN argN) {
... //catch block
}
catch(...) //用于捕获任何异常,所以切记要把它放最后一个.
{
... //catch block
}
try-catch 使用例子:
1 #include <iostream> 2 using namespace std; 3 4 void main() 5 { 6 double x(0),y(0),z(0); 7 cout<<"enter two number:"<<endl; 8 9 while(cin>>x>>y) 10 { 11 try{ 12 if(x == -y) 13 throw "x == -y isn't allowed!"; 14 z = 2.0*x*y/(x+y); 15 } 16 catch(const char* s) 17 { 18 cout<<s<<endl; 19 cout<<"Enter a new pair of numbers:\n"; 20 continue; 21 } 22 std::cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<endl; 23 std::cout<<"Enter next set of numbers< q to quit >:\n"; 24 } 25 cout<<"Bye!\n"; 26 system("pause"); 27 }
输出结果:
一个函数引发多个异常:
1 #include <iostream> 2 using namespace std; 3 4 void hmean(double x,double y)throw(const char*,int) 5 { 6 if(x > y) 7 throw "x > y isn't allowed!"; 8 if(x<0 || y<0) 9 throw 0; 10 } 11 void main() 12 { 13 double x(0),y(0),z(0); 14 cout<<"enter two number:"<<endl; 15 16 while(cin>>x>>y) 17 { 18 try{ 19 hmean(x,y); 20 cout<<"these numbers is ok!"<<endl; 21 } 22 catch(const char* s) 23 { 24 cout<<s<<endl; 25 cout<<"Enter a new pair of numbers:\n"; 26 continue; 27 } 28 catch(int) 29 { 30 cout<<"invalid number!"<<endl; 31 cout<<"Enter a new pair of numbers(greater than zero):\n"; 32 continue; 33 } 34 std::cout<<"Enter next set of numbers< q to quit >:\n"; 35 } 36 system("pause"); 37 }
输出结果:
工作流程:
throw-catch 块是可以嵌套分层的,并通过异常对象的数据类型来进行匹配,以找到正确的 catch-block 异常错误处理代码,过程如下:
[返回子目录]
流程图如下:
在一个异常捕获的中的 try-catch 搜索过程:
(所谓的第一个 catch-block表示的是在 try 语句块后第一个紧跟的 catch-block,第二第三 catch-block 也是根据 try 位置而定)
例子:
例1 -- 基类在子类之前
1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 public: 7 void show(){cout<<"this is class A"<<endl;} 8 }; 9 class B:public A 10 { 11 public: 12 void show(){cout<<"this is class B"<<endl;} 13 }; 14 class C:public B 15 { 16 public: 17 void show(){cout<<"this is class C"<<endl;} 18 }; 19 void main() 20 { 21 int i; 22 A a; 23 B b; 24 C c; 25 cout<<"enter a number(0 is throw class A; 1 is throw class B; 2 is throw class C):\n"; 26 while(cin>>i) 27 { 28 try{ 29 switch(i) 30 { 31 case 0: 32 throw a; 33 break; 34 case 1: 35 throw b; 36 break; 37 case 2: 38 throw c; 39 break; 40 default: 41 cout<<"the number is invalid!"<<endl; 42 } 43 } 44 catch(A a) 45 { 46 a.show(); 47 } 48 catch(B a) 49 { 50 a.show(); 51 } 52 catch(C a) 53 { 54 a.show(); 55 } 56 cout<<"enter a number(0 is throw class A; 1 is throw class B; 2 is throw class C):\n"; 57 } 58 system("pause"); 59 }
输出结果:
例2 -- 基类在子类之后
1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 public: 7 void show(){cout<<"this is class A"<<endl;} 8 }; 9 class B:public A 10 { 11 public: 12 void show(){cout<<"this is class B"<<endl;} 13 }; 14 class C:public B 15 { 16 public: 17 void show(){cout<<"this is class C"<<endl;} 18 }; 19 void main() 20 { 21 int i; 22 A a; 23 B b; 24 C c; 25 cout<<"enter a number(0 is throw class A; 1 is throw class B; 2 is throw class C):\n"; 26 while(cin>>i) 27 { 28 try{ 29 switch(i) 30 { 31 case 0: 32 throw a; 33 break; 34 case 1: 35 throw b; 36 break; 37 case 2: 38 throw c; 39 break; 40 default: 41 cout<<"the number is invalid!"<<endl; 42 } 43 } 44 45 catch(C a) 46 { 47 a.show(); 48 } 49 catch(B a) 50 { 51 a.show(); 52 } 53 catch(A a) 54 { 55 a.show(); 56 } 57 cout<<"enter a number(0 is throw class A; 1 is throw class B; 2 is throw class C):\n"; 58 } 59 system("pause"); 60 }
输出结果:
在多个异常捕获的嵌套中的 try-catch 搜索过程:
例子:
1 #include <iostream> 2 using namespace std; 3 4 void main() 5 { 6 int i; 7 cout<<"enter a number(0 is throw_int; 1 is throw_double; 2 is throw_char):\n"; 8 while(cin>>i) 9 { 10 try{ 11 try{ 12 switch(i) 13 { 14 case 0: 15 throw 0; 16 break; 17 case 1: 18 throw 1.1; 19 break; 20 case 2: 21 throw 'a'; 22 break; 23 default: 24 cout<<"the number is invalid!"<<endl; 25 } 26 } 27 catch(char) 28 { 29 cout<<"in the in_catch(char)!"<<endl; 30 } 31 catch(double) 32 { 33 cout<<"in the catch(double)!"<<endl; 34 } 35 } 36 catch(int) 37 { 38 cout<<"in the catch(int)!"<<endl; 39 } 40 catch(char) 41 { 42 cout<<"in the out_catch(char)!"<<endl; 43 } 44 cout<<"enter a number(0 is throw_int; 1 is throw_double; 2 is throw_char):\n"; 45 } 46 system("pause"); 47 }
输出结果:
[返回子目录]
异常机制与函数返回机制区别:
虽然 throw-catch 机制类似于函数参数和函数返回机制,但是还是有写不同之处.其中之一是函数中的返回语句将控制权返回到调用函数的函数,但是 throw 语句将函数控制权向上返回到第一个这样的函数:包含能够捕获相应异常的 try-catch 组合.
执行流程的区别:
[返回子目录]
异常捕获的优点:
[返回子目录]
异常处理中需要注意的问题:
[返回子目录]
将对象作为异常类型:
可以使用不同的异常类型来区分不同的函数在不同情况下引发的异常另外对象可以携带信息程序员可以根据这些信息来确定引发异常的原因.并且建议传递异常类型用基类的引用或者指针.因为基类的引用或者指针有一个重要的特性:基类引用或者指针可以执行派生类对象.也就是说当有一个异常类层次结构,并要分别处理不同的异常类型,则使用基类引用将能够捕获基类和由基类派生出的派生类异常对象,而使用派生类对象只能捕获他所属类以及这个类派生而来的类的对象.
[返回子目录]
异常规范:
说明:
指出它将可能引发那些类型的异常为此可在函数定义后面加上异常规范
声明形式:
返回类型 函数名(形参列表)throw(异常类型1异常类型2 ... )
注意:
例子:
1 #include <iostream> 2 using namespace std; 3 4 double hmean(double x,double y)throw(const char*) 5 { 6 if(x == -y) 7 throw "x == -y isn't allowed!"; 8 return 2.0*x*y/(x+y); 9 } 10 void main() 11 { 12 double x(0),y(0),z(0); 13 cout<<"enter two number:"<<endl; 14 15 while(cin>>x>>y) 16 { 17 try{ 18 z = hmean(x,y); 19 } 20 catch(const char* s) 21 { 22 cout<<s<<endl; 23 cout<<"Enter a new pair of numbers:\n"; 24 continue; 25 } 26 std::cout<<"Harmonic mean of "<<x<<" and "<<y<<" is "<<z<<endl; 27 std::cout<<"Enter next set of numbers< q to quit >:\n"; 28 } 29 cout<<"Bye!\n"; 30 system("pause"); 31 }
[返回子目录]
exception 类:
位于 exception 头文件中,它可以作用于其他异常类的基类,并且有一个名为 what() 的成员函数,它返回一个字符串,该字符串的特性随派生类的实现而异.
stdexcept 异常类
头文件 stdexcept 定义了其他几个异常类.
首先该类定义了 logic_error 和 runtime_error 类
logic_error 异常系列描述了典型的逻辑错误.其派生类有:
runtime_error 异常系列描述了可能在运行期间发生但难以预见和防范的错误.其派生类有:
[返回子目录]
bad_alloc 异常类:
在处理使用 new 是可能出现的内存分配问题. C++ 提供了两种可能选择的方式.实现只能有一种方式.但也可以使用编译器开关或者其他一些方法,让编程者能够选择喜欢的方式
[返回目录]