update
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# 异常处理
|
||||
1.catch捕获异常时,不会进行数据类型的默认转换。
|
||||
2.限制异常的方法
|
||||
- 当一个函数声明中不带任何异常描述时,它可以抛出任何异常。例如:
|
||||
```c++
|
||||
int f(int,char); //函数f可以抛出任何异常
|
||||
```
|
||||
- 在函数声明的后面添加一个throw参数表,在其中指定函数可以抛出的异常类型。例如:
|
||||
```c++
|
||||
int g(int,char) throw(int,char); //只允许抛出int和char异常。
|
||||
```
|
||||
- 指定throw限制表为不包括任何类型的空表,不允许函数抛出任何异常。如:
|
||||
```c++
|
||||
int h(int,char) throw();//不允许抛出任何异常
|
||||
```
|
||||
3.捕获所有异常
|
||||
在多数情况下,catch都只用于捕获某种特定类型的异常,但它也具有捕获全部异常的能力。其形式如下:
|
||||
```c++
|
||||
catch(…) {
|
||||
…… //异常处理代码
|
||||
}
|
||||
```
|
||||
4.再次抛出异常
|
||||
如是catch块无法处理捕获的异常,它可以将该异常再次抛出,使异常能够在恰当的地方被处理。再次抛出的异常不会再被同一个catch块所捕获,它将被传递给外部的catch块处理。要在catch块中再次抛出同一异常,只需在该catch块中添加不带任何参数的throw语句即可。
|
||||
5.异常的嵌套调用
|
||||
try块可以嵌套,即一个try块中可以包括另一个try块,这种嵌套可能形成一个异常处理的调用链。
|
||||
@@ -0,0 +1,19 @@
|
||||
//Eg10-1.cpp
|
||||
#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");
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//Eg10-11.cpp
|
||||
#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");
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
//Eg10-2.cpp
|
||||
#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) { //仅此与例10.1不同
|
||||
cout<<"4--In catch block1 .. an int type is.."<<i<<endl;
|
||||
}
|
||||
cout<<"5--After Catch...";
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void temperature(int t)
|
||||
{
|
||||
try{
|
||||
if(t==100) throw "沸点!";
|
||||
else if(t==0) throw "冰点!";
|
||||
else cout<<"the temperature is OK..."<<endl;
|
||||
}
|
||||
catch(int x){cout<<"temperatore="<<x<<endl;}
|
||||
catch(char const*s){cout<<s<<endl;}
|
||||
}
|
||||
int main(){
|
||||
temperature(0); //L1
|
||||
temperature(10); //L2
|
||||
temperature(100); //L3
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void temperature(int t)
|
||||
{
|
||||
|
||||
if(t==100) throw "沸点!";
|
||||
else if(t==0) throw "冰点!";
|
||||
else{cout<<"temperatore="<<t<<endl;}
|
||||
|
||||
}
|
||||
int main(){
|
||||
try{
|
||||
temperature(0); //L1
|
||||
temperature(10); //L2
|
||||
temperature(100); //L3
|
||||
}
|
||||
catch(char const*s){cout<<s<<endl;}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//Eg10-5.cpp
|
||||
#include<iostream>
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//Eg10-6.cpp
|
||||
#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;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
//Eg10-9.cpp
|
||||
#include<iostream>
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//Eg10-7.cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//内部再次throw异常的时候,函数不要带throw()
|
||||
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; //再次抛出本catch捕获的异常
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
try{
|
||||
Errhandler(1);
|
||||
}
|
||||
catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
|
||||
cout<<"....End..."<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//Eg10-10.cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int MAX=3;
|
||||
class Full{}; //L1 堆栈满时抛出的异常类
|
||||
class Empty{}; //L2 堆栈空时抛出的异常类
|
||||
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);
|
||||
//s.push(40); //L5 将产生栈满异常
|
||||
cout<<"stack(0)= "<<s.pop()<<endl;
|
||||
cout<<"stack(1)= "<<s.pop()<<endl;
|
||||
cout<<"stack(2)= "<<s.pop()<<endl;
|
||||
cout<<"stack(3)= "<<s.pop()<<endl; //L6
|
||||
}
|
||||
catch(Full){ cout<<"Exception: Stack Full"<<endl; }
|
||||
catch(Empty){ cout<<"Exception: Stack Empty"<<endl; }
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
//Eg10-12.cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
class BasicException{
|
||||
public:
|
||||
char* Where(){return "BasicException...";}
|
||||
};
|
||||
class FileSysException:public BasicException{
|
||||
public:
|
||||
char *Where(){return "FileSysException...";}
|
||||
};
|
||||
class FileNotFound:public FileSysException{
|
||||
public:
|
||||
char *Where(){return "FileNotFound...";}
|
||||
};
|
||||
class DiskNotFound:public FileSysException{
|
||||
public:
|
||||
char *Where(){return "DiskNotFound...";}
|
||||
};
|
||||
void 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;}
|
||||
}
|
||||
Reference in New Issue
Block a user