update
This commit is contained in:
19
practical_exercises/10_day_practice/day9/异常例子/1.cpp
Normal file
19
practical_exercises/10_day_practice/day9/异常例子/1.cpp
Normal file
@@ -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");
|
||||
}
|
42
practical_exercises/10_day_practice/day9/异常例子/10.cpp
Normal file
42
practical_exercises/10_day_practice/day9/异常例子/10.cpp
Normal file
@@ -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");
|
||||
}
|
17
practical_exercises/10_day_practice/day9/异常例子/2.cpp
Normal file
17
practical_exercises/10_day_practice/day9/异常例子/2.cpp
Normal file
@@ -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) { //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>10.1<EFBFBD><EFBFBD>ͬ
|
||||
cout<<"4--In catch block1 .. an int type is.."<<i<<endl;
|
||||
}
|
||||
cout<<"5--After Catch...";
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
19
practical_exercises/10_day_practice/day9/异常例子/3.cpp
Normal file
19
practical_exercises/10_day_practice/day9/异常例子/3.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void temperature(int t)
|
||||
{
|
||||
try{
|
||||
if(t==100) throw "<EFBFBD>е㣡";
|
||||
else if(t==0) throw "<EFBFBD><EFBFBD><EFBFBD>㣡";
|
||||
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;
|
||||
}
|
20
practical_exercises/10_day_practice/day9/异常例子/4.cpp
Normal file
20
practical_exercises/10_day_practice/day9/异常例子/4.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
void temperature(int t)
|
||||
{
|
||||
|
||||
if(t==100) throw "<EFBFBD>е㣡";
|
||||
else if(t==0) throw "<EFBFBD><EFBFBD><EFBFBD>㣡";
|
||||
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;
|
||||
}
|
19
practical_exercises/10_day_practice/day9/异常例子/5.cpp
Normal file
19
practical_exercises/10_day_practice/day9/异常例子/5.cpp
Normal file
@@ -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");
|
||||
}
|
18
practical_exercises/10_day_practice/day9/异常例子/6.cpp
Normal file
18
practical_exercises/10_day_practice/day9/异常例子/6.cpp
Normal file
@@ -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;
|
||||
}
|
33
practical_exercises/10_day_practice/day9/异常例子/7-1.cpp
Normal file
33
practical_exercises/10_day_practice/day9/异常例子/7-1.cpp
Normal file
@@ -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");
|
||||
}
|
24
practical_exercises/10_day_practice/day9/异常例子/7.cpp
Normal file
24
practical_exercises/10_day_practice/day9/异常例子/7.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//Eg10-7.cpp
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//<2F>ڲ<EFBFBD><DAB2>ٴ<EFBFBD>throw<6F>쳣<EFBFBD><ECB3A3>ʱ<EFBFBD><EFBFBD><F2A3ACBA><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>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; //<2F>ٴ<EFBFBD><D9B4>׳<EFBFBD><D7B3><EFBFBD>catch<63><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>쳣
|
||||
}
|
||||
}
|
||||
int main(){
|
||||
try{
|
||||
Errhandler(1);
|
||||
}
|
||||
catch(int x){ cout<<"catch int an exception in main..."<<x<<endl; }
|
||||
cout<<"....End..."<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
39
practical_exercises/10_day_practice/day9/异常例子/8.cpp
Normal file
39
practical_exercises/10_day_practice/day9/异常例子/8.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
//Eg10-10.cpp
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
const int MAX=3;
|
||||
class Full{}; //L1 <20><>ջ<EFBFBD><D5BB>ʱ<EFBFBD>׳<EFBFBD><D7B3><EFBFBD><EFBFBD>쳣<EFBFBD><ECB3A3>
|
||||
class Empty{}; //L2 <20><>ջ<EFBFBD><D5BB>ʱ<EFBFBD>׳<EFBFBD><D7B3><EFBFBD><EFBFBD>쳣<EFBFBD><ECB3A3>
|
||||
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 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD>쳣
|
||||
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;
|
||||
}
|
37
practical_exercises/10_day_practice/day9/异常例子/9.cpp
Normal file
37
practical_exercises/10_day_practice/day9/异常例子/9.cpp
Normal file
@@ -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{
|
||||
..... //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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{
|
||||
..... //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
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