《C++捷径教程》读书笔记--Chapter 17--异常处理(完结)

//--《C++捷径教程》读书笔记--Chapter 17--异常处理(完结)
//--Chapter 17--异常处理
//--04/17/2006 Mon.
//--Computer Lab
//--Liwei

 

//--程序#1  异常处理
#include <iostream>
using namespace std;

int main()
{
 cout<<"start/n";
 
 try{
  cout<<"Inside try block/n";
  throw 99;
  cout<<"This will not execute";
 }

 catch(int i){
  cout<<"Caught an exception--value is: "<<i<<endl;
 }

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#2  异常处理
#include <iostream>
using namespace std;

int main()
{
 cout<<"start/n";
 
 try{
  cout<<"Inside try block/n";
  throw 99;
  cout<<"This will not execute";
 }

 catch(double i){
  cout<<"Caught an exception--value is: "<<i<<endl;
 }

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#3  异常处理
#include <iostream>
using namespace std;

void Xtest(int test)
{
 cout<<"Inside Xtest, test is: "<<test<<endl;
 if(test) throw test;
}

int main()
{
 cout<<"start/n";
 
 try{
  cout<<"Inside try block/n";
  Xtest(0);
  Xtest(1);
  Xtest(2);
 }

 catch(double i){
  cout<<"Caught an exception--value is: "<<i<<endl;
 }

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#4  异常处理
#include <iostream>
using namespace std;

void Xtest(int test)
{
 //cout<<"Inside Xtest, test is: "<<test<<endl;
 try{
  if(test) throw test;
 }

 catch(int i){
  cout<<"Caught an exception--value is: "<<i<<endl;
 }
}

int main()
{
 cout<<"start/n";
 
 Xtest(1);
 Xtest(2);
 Xtest(0);
 Xtest(3);

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#5  异常处理
#include <iostream>
#include <cstring>
using namespace std;

class Myexception{
public:
 char str_what[80];
 Myexception() { *str_what=0; }
 Myexception(char *s) { strcpy(str_what,s); }
};

int main()
{
 int a,b;

 try{
  cout<<"Enter numerator and denominator: ";
  cin>>a>>b;
  if(!b)  throw Myexception("Cannot divide by zero!");
  else  cout<<"Quotient is: "<<a/b<<endl;
 }

 catch(Myexception e){
  cout<<e.str_what<<endl;
 }

 return 0;

}

//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#6  异常处理
#include <iostream>
#include <cstring>
using namespace std;

void Xhandler(int test)
{
 try{
  if(test)  throw test;
  else throw "Value is zero.";
 }

 catch(int i){
  cout<<"Caught one! Ex.#: "<<i<<endl;
 }

 catch(char *str){
  cout<<"Caught a string: "<<str<<endl;
 }
}

int main()
{
 cout<<"start/n";

 Xhandler(1);
 Xhandler(2);
 Xhandler(0);
 Xhandler(3);

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#7  异常处理
#include <iostream>
#include <cstring>
using namespace std;

class B{
};

class D:public B{
};

int main()
{
 D derived;

 try{
  throw derived;
 }

 catch(B b){
  cout<<"Caught a base class./n";
 }

 catch(D d){
  cout<<"This won't execute./n";
 }

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#8  异常处理
#include <iostream>
#include <cstring>
using namespace std;

void Xhandler(int test)
{
 try{
  if(test==0)  throw test;
  if(test==1)  throw 'a';
  if(test==2)  throw 123.23;
 }

 catch(...){
  cout<<"Caught one! Ex.#: "<<endl;
 }

 //catch(char *str){
 // cout<<"Caught a string: "<<str<<endl;
 //}
}

int main()
{
 cout<<"start/n";

 Xhandler(0);
 Xhandler(1);
 Xhandler(2);

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#9  异常处理
#include <iostream>
#include <cstring>
using namespace std;

void Xhandler(int test)
{
 try{
  if(test==0)  throw test;
  if(test==1)  throw 'a';
  if(test==2)  throw 123.23;
 }

 catch(int i){
  cout<<"Caught "<<i<<endl;
 }

 catch(...){
  cout<<"Caught one! Ex.#: "<<endl;
 }


}

int main()
{
 cout<<"start/n";

 Xhandler(0);
 Xhandler(1);
 Xhandler(2);

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#10  异常处理
#include <iostream>
#include <cstring>
using namespace std;

void Xhandler(int test) throw(int, char ,double)
{
 if(test==0)  throw test;
 if(test==1)  throw 'a';
 if(test==2)  throw 123.23;
}

int main()
{
 cout<<"start/n";

 try{
  //Xhandler(0);
  //Xhandler(1);
  Xhandler(2);
 }

 catch(int i){
  cout<<"Caught int./n";
 }

 catch(char c){
  cout<<"Caught char./n";
 }

 catch(double d){
  cout<<"Caught double./n";
 }

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#11  异常处理
#include <iostream>
#include <cstring>
using namespace std;

void Xhandler()
{
 try{
  throw "hello";
 }

 catch(char *){
  cout<<"Caught char * inside Xhandler./n";
  throw;
 }
}

int main()
{
 cout<<"start/n";

 try{
  Xhandler();
 }

 catch(char *){
  cout<<"Caught char * inside main./n";
 }

 cout<<"end/n";

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#12  异常处理
#include <iostream>
#include <new>
using namespace std;

int main()
{
 int *p, i;

 try{
  p=new int[32];
 }

 catch(bad_alloc xa){
  cout<<"Allocation failure./n";
  return 1;
 }

 for(i=0; i<32; i++)
  p[i]=i;

 for(i=0; i<32; i++)
  cout<<p[i]<<' ';
 cout<<endl;

 delete []p;

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//
//--程序#13  异常处理
#include <iostream>
#include <new>
using namespace std;

int main()
{
 int *p, i;

 p=new(nothrow) int[32];
 if(!p){
  cout<<"Allocation failure./n";
  return 1;
 }

 for(i=0; i<32; i++)
  p[i]=i;

 for(i=0; i<32; i++)
  cout<<p[i]<<' ';
 cout<<endl;

 delete []p;

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//

//--程序#14  异常处理
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;

class three_d{
 int x,y,z;
public:
 three_d() { x=y=z=0; cout<<"Constructing 0,0,0 /n"; }
 three_d(int i, int j, int k) { x=i; y=j; z=k; cout<<"Constructing "<<i<<","<<j<<", "<<k<<'/n';}
 ~three_d() { cout<<"Destructing./n"; } 

 void *operator new(size_t size);
 void *operator new[](size_t size);
 void operator delete(void *p);
 void operator delete[](void *p);

 void show();
};

void *three_d::operator new(size_t size)
{
 void *p;
 
 cout<<"Allocating three_d object./n";
 p=malloc(size);
 if(!p){
  bad_alloc ba;
  throw ba;
 }
 return p;
}

void *three_d::operator new[](size_t size)
{
 void *p;
 
 cout<<"Allocating array of three_d objects./n";
 p=malloc(size);
 if(!p){
  bad_alloc ba;
  throw ba;
 }
 return p;
}
//=================
void three_d::operator delete(void *p)
{
 cout<<"Deleting three_d object./n";
 free(p);
}

void three_d::operator delete[](void *p)
{
 cout<<"Deleting array of three_d objects./n";
 free(p);
}

void three_d::show()
{
 cout<<x<<", ";
 cout<<y<<", ";
 cout<<z<<'/n';
}

int main()
{
 three_d  *p1, *p2;

 try{
  p1=new three_d[3];
  p2=new three_d(5,6,7);
 }

 catch(bad_alloc ba){
  cout<<"Allocation error./n";
  return 1;
 }

 p1[1].show();
 p2->show();

 delete []p1;
 delete p2;

 cout<<endl<<endl;

 return 0;
}
//=================================================================//
//==============================END================================//
//=================================================================//

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值