-
Notifications
You must be signed in to change notification settings - Fork 33
/
18.03.cpp
57 lines (52 loc) · 1.24 KB
/
18.03.cpp
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
/*
* Exercise 18.3: There are two ways to make the previous code work correctly
* if an exception is thrown. Describe them and implement them.
*
* By Faisal Saadatmand
*/
/*
* (1) Handle the exception locally in order to free p.
* (2) Use a smart pointer instead.
*/
#include <fstream>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <vector>
// 1. Handle exception
void exercise_1(int *b, int *e) {
int *p;
try {
std::vector<int> v(b, e);
p = new int[v.size()];
std::ifstream in("ints");
throw std::ios_base::failure("ifstream"); // simulate ifstream error
} catch (std::exception &r) {
delete[] p; // free allocate memory before rethrowing
throw;
}
}
// 2. Use a unique pointer
void exercise_2(int *b, int *e) {
std::vector<int> v(b, e);
std::unique_ptr<int[]> p(new int[v.size()]);
std::ifstream in("ints");
throw std::ios_base::failure("ifstream"); // simulate ifstream error
}
int main()
{
int a[10] = {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
try {
exercise_1(a, &a[10]);
}
catch (std::exception &error) {
std::cerr << "exercise_1: " << error.what() << std::endl;
}
try {
exercise_2(a, &a[10]);
}
catch (std::exception &error) {
std::cerr << "exercise_2: " << error.what() << std::endl;
}
return 0;
}