support bazel complie this project and format code.
This commit is contained in:
17
practical_exercises/10_day_practice/day9/exception/1.cpp
Normal file
17
practical_exercises/10_day_practice/day9/exception/1.cpp
Normal 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...";
|
||||
|
||||
}
|
||||
43
practical_exercises/10_day_practice/day9/exception/10.cpp
Normal file
43
practical_exercises/10_day_practice/day9/exception/10.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
16
practical_exercises/10_day_practice/day9/exception/2.cpp
Normal file
16
practical_exercises/10_day_practice/day9/exception/2.cpp
Normal 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;
|
||||
}
|
||||
23
practical_exercises/10_day_practice/day9/exception/3.cpp
Normal file
23
practical_exercises/10_day_practice/day9/exception/3.cpp
Normal 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;
|
||||
}
|
||||
23
practical_exercises/10_day_practice/day9/exception/4.cpp
Normal file
23
practical_exercises/10_day_practice/day9/exception/4.cpp
Normal 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;
|
||||
}
|
||||
25
practical_exercises/10_day_practice/day9/exception/5.cpp
Normal file
25
practical_exercises/10_day_practice/day9/exception/5.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
22
practical_exercises/10_day_practice/day9/exception/6.cpp
Normal file
22
practical_exercises/10_day_practice/day9/exception/6.cpp
Normal 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;
|
||||
}
|
||||
34
practical_exercises/10_day_practice/day9/exception/7-1.cpp
Normal file
34
practical_exercises/10_day_practice/day9/exception/7-1.cpp
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
24
practical_exercises/10_day_practice/day9/exception/7.cpp
Normal file
24
practical_exercises/10_day_practice/day9/exception/7.cpp
Normal 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;
|
||||
}
|
||||
45
practical_exercises/10_day_practice/day9/exception/8.cpp
Normal file
45
practical_exercises/10_day_practice/day9/exception/8.cpp
Normal 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;
|
||||
}
|
||||
27
practical_exercises/10_day_practice/day9/exception/9-2.cpp
Normal file
27
practical_exercises/10_day_practice/day9/exception/9-2.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
45
practical_exercises/10_day_practice/day9/exception/9.cpp
Normal file
45
practical_exercises/10_day_practice/day9/exception/9.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
63
practical_exercises/10_day_practice/day9/exception/BUILD
Normal file
63
practical_exercises/10_day_practice/day9/exception/BUILD
Normal 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"],
|
||||
)
|
||||
@@ -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); //只允许抛出int和char异常。
|
||||
```
|
||||
- ָ<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块,这种嵌套可能形成一个异常处理的调用链。
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;}
|
||||
}
|
||||
Reference in New Issue
Block a user