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"],
|
||||
)
|
Reference in New Issue
Block a user