forked from toksaitov/simple-notepad-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exceptions.cpp
More file actions
81 lines (67 loc) · 2.41 KB
/
Copy pathtest_exceptions.cpp
File metadata and controls
81 lines (67 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "notepad_exception.h"
#include <iostream>
#include <stdexcept>
#include <string>
void test_single_catch() {
std::cout << "Test 1: Single catch\n";
try {
throw file_not_found_exception("missing.txt");
} catch (const notepad_exception &ex) {
std::cout << "Caught notepad_exception: " << ex.what() << "\n";
}
std::cout << "\n";
}
void test_multiple_catches() {
std::cout << "Test 2: Multiple catches\n";
try {
throw file_not_found_exception("missing.txt");
} catch (const file_not_found_exception &ex) {
std::cout << "Test 2a: Caught file_not_found_exception: " << ex.what() << "\n";
} catch (const notepad_exception &ex) {
std::cout << "Test 2a: Caught notepad_exception: " << ex.what() << "\n";
} catch (const std::exception &ex) {
std::cout << "Test 2a: Caught std::exception: " << ex.what() << "\n";
}
try {
throw file_read_exception("corrupt.dat");
} catch (const file_not_found_exception &ex) {
std::cout << "Test 2b: Caught file_not_found_exception: " << ex.what() << "\n";
} catch (const notepad_exception &ex) {
std::cout << "Test 2b: Caught notepad_exception: " << ex.what() << "\n";
} catch (const std::exception &ex) {
std::cout << "Test 2b: Caught std::exception: " << ex.what() << "\n";
}
try {
throw std::runtime_error("Unknown error");
} catch (const file_not_found_exception &ex) {
std::cout << "Test 2c: Caught file_not_found_exception: " << ex.what() << "\n";
} catch (const notepad_exception &ex) {
std::cout << "Test 2c: Caught notepad_exception: " << ex.what() << "\n";
} catch (const std::exception &ex) {
std::cout << "Test 2c: Caught std::exception: " << ex.what() << "\n";
}
std::cout << "\n";
}
void open_file_inner(const std::string &filename) {
try {
throw file_not_found_exception(filename);
} catch (const notepad_exception &ex) {
std::cout << "Inner catch in open_file: " << ex.what() << "\n";
std::cout << "Rethrowing...\n";
throw;
}
}
void test_rethrow() {
std::cout << "Test 3: Rethrow\n";
try {
open_file_inner("missing.txt");
} catch (const notepad_exception &ex) {
std::cout << "Outer catch in main: " << ex.what() << "\n";
}
}
int main() {
test_single_catch();
test_multiple_catches();
test_rethrow();
return 0;
}