2.C++异常处理学习
1.catch捕获异常时,不会进行数据类型的默认转换
1.1 能够正常捕获异常
#include<iostream>
using namespace std;
int main(){
cout<<"1--befroe try block..."<<endl;
try{
cout<<"2--Inside try block..."<<endl;
throw 10;
cout<<"3--After throw ...."<<endl;
}
catch(int i) {
cout<<"4--In catch block1 ... exception..errcode is.."<<i<<endl;
}
catch(char * s) {
cout<<"5--In catch block2 ... exception..errcode is.."<<s<<endl;
}
cout<<"6--After Catch...";
system("pause");
}
1--befroe try block...
2--Inside try block...
4--In catch block1 ... exception..errcode is..10
6--After Catch...
1.2 不能进行隐式转换捕获异常
#include<iostream>
using namespace std;
int main(){
cout<<"1--befroe try block..."<<endl;
try{
cout<<"2--Inside try block..."<<endl;
throw 10;
cout<<"3--After throw ...."<<endl;
}
catch(double i) {
cout<<"4--In catch block1 .. an int type is.."<<i<<endl;
}
cout<<"5--After Catch...";
system("pause");
return 0;
}
1--befroe try block...
2--Inside try block...
terminate called after throwing an instance of 'int'
2.try块中一旦抛出异常则不再执行下方代码
#include<iostream>
using namespace std;
void temperature(int t)
{
if(t==100) throw "the temperature is so hot";
else if(t==0) throw "the temperature is so cool";
else{cout<<"temperatore="<<t<<endl;}
}
int main(){
try{
temperature(0);
temperature(10);
temperature(100);
}
catch(char const*s){cout<<s<<endl;}
system("pause");
return 0;
}
the temperature is so cool
3.限制异常的方法
- 当一个函数声明中不带任何异常描述时,它可以抛出任何异常。
int f(int,char);
- 在函数声明的后面添加一个throw参数表,在其中指定函数可以抛出的异常类型。
int g(int,char) throw(int,char);
- 指定throw限制表为不包括任何类型的空表,不允许函数抛出任何异常。
int h(int,char) throw();
#include<iostream>
using namespace std;
void Errhandler(int n)
{
try{
if(n==1) throw n;
cout<<"all is ok..."<<endl;
}
catch(int n){
cout<<"catch an int exception inside..."<<n<<endl;
throw n;
}
}
int main(){
try{
Errhandler(1);
}
catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
cout<<"....End..."<<endl;
system("pause");
return 0;
}
catch an int exception inside...1
catch int an exception in main...1
....End...
4.catch捕获异常类型
catch(Exception e);
catch(...);
#include<iostream>
using namespace std;
void Errhandler(int n)throw(){
try{
if(n==1) throw n;
if(n==2) throw "dx";
if(n==3) throw 1.1;
}
catch(...){cout<<"catch an exception..."<<endl;}
}
int main(){
Errhandler(1);
Errhandler(2);
Errhandler(3);
system("pause");
return 0;
}
catch an exception...
catch an exception...
catch an exception...
5.catch中可继续抛出异常,等待外部处理
#include<iostream>
using namespace std;
void Errhandler(int n)
{
try{
if(n==1) throw n;
cout<<"all is ok..."<<endl;
}
catch(int n){
cout<<"catch an int exception inside..."<<n<<endl;
throw n;
}
}
int main(){
try{
Errhandler(1);
}
catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
cout<<"....End..."<<endl;
system("pause");
return 0;
}
catch an int exception inside...1
catch int an exception in main...1
....End...
再次抛出异常,如果是catch块无法处理捕获的异常,它可以将该异常再次抛出,使异常能够在恰当的地方被处理。再次抛出的异常不会再被同一个catch块所捕获,它将被传递给外部的catch块处理。要在catch块中再次抛出同一异常,只需在该catch块中添加不带任何参数的throw语句即可。
6.异常类
6.1可自定义异常类
#include <iostream>
using namespace std;
const int MAX=3;
class Full{};
class Empty{};
class Stack{
private:
int s[MAX];
int top;
public:
void push(int a);
int pop();
Stack(){top=-1;}
};
void Stack::push(int a){
if(top>=MAX-1)
throw Full();
s[++top]=a;
}
int Stack::pop(){
if(top<0) throw Empty();
return s[top--];
}
int main(){
Stack s;
try{
s.push(10); s.push(20); s.push(30);
cout<<"stack(0)= "<<s.pop()<<endl;
cout<<"stack(1)= "<<s.pop()<<endl;
cout<<"stack(2)= "<<s.pop()<<endl;
cout<<"stack(3)= "<<s.pop()<<endl;
}
catch(Full){ cout<<"Exception: Stack Full"<<endl; }
catch(Empty){ cout<<"Exception: Stack Empty"<<endl; }
system("pause");
return 0;
}
stack(0)= 30
stack(1)= 20
stack(2)= 10
stack(3)= Exception: Stack Empty
6.2异常类的捕获
派生异常类无法捕获基类异常,基类异常类可捕获派生类异常
#include<iostream>
using namespace std;
class BasicException{
public:
string Where(){return "BasicException...";}
};
class FileSysException:public BasicException{
public:
string Where(){return "FileSysException...";}
};
class FileNotFound:public FileSysException{
public:
string Where(){return "FileNotFound...";}
};
class DiskNotFound:public FileSysException{
public:
string Where(){return "DiskNotFound...";}
};
int main(){
try{
throw FileSysException();
}
catch(DiskNotFound p){cout<<p.Where()<<endl;}
catch(FileNotFound p){cout<<p.Where()<<endl;}
catch(FileSysException p){cout<<p.Where()<<endl;}
catch(BasicException p){cout<<p.Where()<<endl;}
try{
throw DiskNotFound();
}
catch(BasicException p){cout<<p.Where()<<endl;}
catch(FileSysException p){cout<<p.Where()<<endl;}
catch(DiskNotFound p){cout<<p.Where()<<endl;}
catch(FileNotFound p){cout<<p.Where()<<endl;}
}
FileSysException...
BasicException...
6.3异常类多态
#include <iostream>
using namespace std;
class BasicException
{
public:
virtual string Where() { return "BasicException..."; }
};
class FileSysException : public BasicException
{
public:
virtual string Where() { return "FileSysException..."; }
};
class FileNotFound : public FileSysException
{
public:
virtual string Where() { return "FileNotFound..."; }
};
class DiskNotFound : public FileSysException
{
public:
virtual string Where() { return "DiskNotFound..."; }
};
int main()
{
try
{
DiskNotFound err;
throw &err;
}
catch (BasicException *p)
{
cout << p->Where() << endl;
}
}
DiskNotFound...
6.4调用异常类成员函数
#include <iostream>
using namespace std;
const int MAX=3;
class Full{
int a;
public:
Full(int i):a(i){}
int getValue(){return a;}
};
class Empty{};
class Stack{
private:
int s[MAX];
int top;
public:
Stack(){top=-1;}
void push(int a){
if(top>=MAX-1)
throw Full(a);
s[++top]=a;
}
int pop(){
if(top<0)
throw Empty();
return s[top--];
}
};
int main(){
Stack s;
try{
s.push(10);
s.push(20);
s.push(30);
s.push(40);
}
catch(Full e){
cout<<"Exception: Stack Full..."<<endl;
cout<<"The value not push in stack:"<<e.getValue()<<endl;
}
system("pause");
}
Exception: Stack Full...
The value not push in stack:40
|