c++ 异常处理简单示例
C++ 异常处理的流程,具体为:
抛出(Throw)–> 检测(Try) --> 捕获(Catch)
异常必须显式地抛出,才能被检测和捕获到;如果没有显式的抛出,即使有异常也检测不到。
#include
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..." << endl;
// system("pause");
return 0;
}
result
1--befroe try block...
2--Inside try block...
4--In catch block1 ... exception..errcode is..10
6--After Catch...
#include
using namespace std;
void temperature(int t)
{
try{
if(t==100) throw "It's at the boiling point.";
else if(t==0) throw "It reached the freezing point";
else cout<<"the temperature is OK..."<<endl;
}
catch(int x){cout<<"temperature="<<x<<endl;}
catch(char const*s){cout<<s<<endl;}
}
int main(){
temperature(0); //L1
temperature(10); //L2
temperature(100); //L3
// system("pause");
return 0;
}
result
It reached the freezing point
the temperature is OK...
It's at the boiling point.
#include
using namespace std;
void temperature(int t)
{
if(t==100) throw "It's at the boiling point.";
else if(t==0) throw "It reached the freezing point";
else cout<<"the temperature is OK..."<<endl;
}
int main(){
try{
temperature(0); //L1
temperature(10); //L2
temperature(100); //L3
}
catch(char const*s){cout<<s<<endl;}
// system("pause");
return 0;
}
result
It reached the freezing point
#include
using namespace std;
void handler(int n)throw(int,char,double){
if(n==1) throw n;
if(n==2) throw 'x';
if(n==3) throw 1.1;
}
int main(){
cout<<"Before handler..."<<endl;
try{
handler(1);
}
catch(int i){ cout<<"catch an integer..."<<endl;}
catch(char c){cout<<"catch an char..."<<endl;}
catch(double d){cout<<"catch an double..."<<endl;}
// system("pause");
return 0;
}
Before handler...
catch an integer...
#include
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;
}
result
catch an exception...
catch an exception...
catch an exception...
//Eg10-9.cpp
#include
using namespace std;
class A{
int a;
public:
A(int i=0):a(i){}
~A(){cout<<"in A destructor..."<<endl;}
};
class B{
A obj[3];
double *pb[10];
public:
B(int k){
cout<<"int B constructor..."<<endl;
for (int i=0;i<10;i++){
pb[i]=new double[20000000];
if(pb[i]==0)
throw i;
else
cout<<"Allocated 20000000 doubles in pb["<<i<<"]"<<endl;
}
}
};
int main(){
try{
B b(2);
}
catch(int e){
cout<<"catch an exception when allocated pb["<<e<<"]"<<endl;
}
system("pause");
}
result
int B constructor...
Allocated 20000000 doubles in pb[0]
Allocated 20000000 doubles in pb[1]
Allocated 20000000 doubles in pb[2]
Allocated 20000000 doubles in pb[3]
Allocated 20000000 doubles in pb[4]
Allocated 20000000 doubles in pb[5]
Allocated 20000000 doubles in pb[6]
Allocated 20000000 doubles in pb[7]
Allocated 20000000 doubles in pb[8]
Allocated 20000000 doubles in pb[9]
in A destructor...
in A destructor...
in A destructor...
栈示例
//Eg10-11.cpp
#include
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");
}
result
Exception: Stack Full...
The value not push in stack:40