现在的位置: 首页 > 综合 > 正文

C++ 异常类 try catch throw…

2012年03月04日 ⁄ 综合 ⁄ 共 550字 ⁄ 字号 评论关闭

//很简单的一个嵌套式的异常        
                                                                            ---------摘自谭浩强的C++.

 

#include <iostream>
using namespace std;

void f1();
void f2();
void f3();
int main()
{
 try
 {
  f1();
 }
 catch (double)
 {
  cout<<"OK0"<<endl;
 }
 cout<<"end0"<<endl;
 getchar();
  return 0;
}
void f1()
{
 try
 {
  f2();
 }
 catch(char)
 {
  cout<<"OK1"<<endl;
 }
 cout<<"end1"<<endl;
}
void f2()
{
 try
 {
  f3();
 }
 catch (int)
 {
  cout<<"OK3"<<endl;
 }
 cout<<"end2"<<endl;
}
void f3()
{
 double a=0;
 try
 {
  throw a;
 }
 catch(double)
 {
  cout<<"OK3"<<endl;
  throw;
 }
 cout<<"end3"<<endl;
}

抱歉!评论已关闭.