-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathItem8.cpp
More file actions
51 lines (42 loc) · 877 Bytes
/
Item8.cpp
File metadata and controls
51 lines (42 loc) · 877 Bytes
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
#include <stdlib.h>
#include <iostream>
using namespace std;
class DestructorDemo
{
public:
DestructorDemo();
void Erase();
~DestructorDemo();
private:
int *a;
};
DestructorDemo::DestructorDemo():
a(0)
{
a = (int*)malloc(sizeof(int));
}
void DestructorDemo::Erase()
{
free(a);
a = 0;
}
DestructorDemo::~DestructorDemo()
{
if ( a )
{
cout << "Throwing an exception" << endl;
// See notes below
throw 10;
}
}
int main()
{
DestructorDemo dd;
}
/* Notes:
* 1). Destructors should never emit exceptions. If functions called in a destructor may throw,
the destructor should catch any exceptions, then swallow them or terminate the program.
*
* 2). If class clients need to be able to react to exceptions thrown during an operation,
the class should provide a regular (i.e., non-destructor) function that performs the operation.
*/