1 #include<iostream.h> 2 int Div(int x,int y); 3 int main() 4 { try 5 { cout<<"5/2="<<Div(5,2)<<endl; 6 cout<<"8/0="<<Div(8,0)<<endl; 7 cout<<"7/1="<<Div(7,1)<<endl; 8 } 9 catch(int) 10 { cout<<"except of deviding zero.\n"; } 11 cout<<"that is ok.\n"; 12 while(1); 13 return 0; 14 } 15 int Div(int x,int y) 16 { if(y==0) throw y; 17 return x/y; 18 } 19 20 /* 21 程序运行结果如下: 22 5/2=2 23 except of deviding zero. 24 that is ok. 25 */
C++异常处理
作者:火星十一郎
本文版权归作者火星十一郎所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.