support bazel complie this project and format code.

This commit is contained in:
zhangxing
2023-03-30 00:15:11 +08:00
committed by light-city
parent 1f86192576
commit 7529ae3a55
636 changed files with 10025 additions and 9387 deletions

View File

@@ -0,0 +1,17 @@
// 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...";
}

View File

@@ -0,0 +1,43 @@
// 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;
}
}

View File

@@ -0,0 +1,16 @@
// 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...";
return 0;
}

View File

@@ -0,0 +1,23 @@
#include <iostream>
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
return 0;
}

View File

@@ -0,0 +1,23 @@
#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;
}
return 0;
}

View File

@@ -0,0 +1,25 @@
// 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;
}
}

View File

@@ -0,0 +1,22 @@
// 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);
return 0;
}

View File

@@ -0,0 +1,34 @@
// 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;
}
}

View File

@@ -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;
return 0;
}

View File

@@ -0,0 +1,45 @@
// 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;
}
return 0;
}

View File

@@ -0,0 +1,27 @@
#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;
}
}

View File

@@ -0,0 +1,45 @@
// 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..."; }
};
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;
}
}

View File

@@ -0,0 +1,63 @@
# please run `bazel run //practical_exercises/10_day_practice/day8/func:3`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:7-1`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:6`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:1`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:9-2`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:10`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:2`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:7`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:5`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:8`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:9`
# please run `bazel run //practical_exercises/10_day_practice/day8/func:4`
load("@rules_cc//cc:defs.bzl", "cc_binary")
cc_binary(
name = "3",
srcs = ["3.cpp"],
)
cc_binary(
name = "7-1",
srcs = ["7-1.cpp"],
)
cc_binary(
name = "6",
srcs = ["6.cpp"],
)
cc_binary(
name = "1",
srcs = ["1.cpp"],
)
cc_binary(
name = "9-2",
srcs = ["9-2.cpp"],
)
cc_binary(
name = "10",
srcs = ["10.cpp"],
)
cc_binary(
name = "2",
srcs = ["2.cpp"],
)
cc_binary(
name = "7",
srcs = ["7.cpp"],
)
cc_binary(
name = "5",
srcs = ["5.cpp"],
)
cc_binary(
name = "8",
srcs = ["8.cpp"],
)
cc_binary(
name = "9",
srcs = ["9.cpp"],
)
cc_binary(
name = "4",
srcs = ["4.cpp"],
)

View File

@@ -1,26 +1,26 @@
# <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
1.catch<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>쳣ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>Ĭ<EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>
- <EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
# 异常处理
1.catch捕获异常时,不会进行数据类型的默认转换。
2.限制异常的方法
- 当一个函数声明中不带任何异常描述时,它可以抛出任何异常。例如:
```c++
int f(int,char); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>f<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD>
int f(int,char); //函数f可以抛出任何异常
```
- <EFBFBD>ں<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>throw<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- 在函数声明的后面添加一个throw参数表在其中指定函数可以抛出的异常类型。例如
```c++
int g(int,char) throw(int,char); //ֻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD>int<EFBFBD><EFBFBD>char<EFBFBD><EFBFBD><EFBFBD>
int g(int,char) throw(int,char); //只允许抛出intchar异常。
```
- ָ<EFBFBD><EFBFBD>throw<EFBFBD><EFBFBD><EFBFBD>Ʊ<EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ŀձ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
- 指定throw限制表为不包括任何类型的空表,不允许函数抛出任何异常。如:
```c++
int h(int,char) throw();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD>
int h(int,char) throw();//不允许抛出任何异常
```
3.<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD>ڶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>catch<EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>
3.捕获所有异常
在多数情况下catch都只用于捕获某种特定类型的异常但它也具有捕获全部异常的能力。其形式如下
```c++
catch(<EFBFBD><EFBFBD>) {
<EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
catch() {
…… //异常处理代码
}
```
4.<EFBFBD>ٴ<EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD>
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>catch<EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>ܹ<EFBFBD><EFBFBD><EFBFBD>ǡ<EFBFBD><EFBFBD><EFBFBD>ĵط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD>׳<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٱ<EFBFBD>ͬһ<EFBFBD><EFBFBD>catch<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><EFBFBD>ⲿ<EFBFBD><EFBFBD>catch<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>catch<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٴ<EFBFBD><EFBFBD>׳<EFBFBD>ͬһ<EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD>catch<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>κβ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>throw<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɡ<EFBFBD>
5.<EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>׵<EFBFBD><EFBFBD><EFBFBD>
try<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>ף<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>try<EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><EFBFBD>԰<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>try<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD>׿<EFBFBD><EFBFBD><EFBFBD><EFBFBD>γ<EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
4.再次抛出异常
如是catch块无法处理捕获的异常它可以将该异常再次抛出使异常能够在恰当的地方被处理。再次抛出的异常不会再被同一个catch块所捕获它将被传递给外部的catch块处理。要在catch块中再次抛出同一异常只需在该catch块中添加不带任何参数的throw语句即可。
5.异常的嵌套调用
try块可以嵌套即一个try块中可以包括另一个try块这种嵌套可能形成一个异常处理的调用链。

View File

@@ -1,19 +0,0 @@
//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");
}

View File

@@ -1,42 +0,0 @@
//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");
}

View File

@@ -1,17 +0,0 @@
//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;
}

View File

@@ -1,19 +0,0 @@
#include<iostream>
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;
}

View File

@@ -1,20 +0,0 @@
#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;
}

View File

@@ -1,19 +0,0 @@
//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");
}

View File

@@ -1,18 +0,0 @@
//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;
}

View File

@@ -1,33 +0,0 @@
//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");
}

View File

@@ -1,24 +0,0 @@
//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;
}

View File

@@ -1,39 +0,0 @@
//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;
}

View File

@@ -1,35 +0,0 @@
#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;
}
}

View File

@@ -1,37 +0,0 @@
//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...";}
};
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;}
}