This repository was archived by the owner on Feb 9, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathsample002.cpp
More file actions
103 lines (80 loc) · 2.56 KB
/
Copy pathsample002.cpp
File metadata and controls
103 lines (80 loc) · 2.56 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) Microsoft Corp 2000. All rights reserved.
//
// Run: Build sample002.sln
//
// Description: Shows how to throw and handle CLR Exceptions in C++
//
///////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <windows.h>
#include <eh.h>
using namespace std;
__declspec(dllimport) void thrower();
class CMyException {
unsigned int m_ErrorCode;
EXCEPTION_POINTERS *m_pExp;
private:
ostream &PrintExceptionRecord(ostream &out, const EXCEPTION_RECORD &er) const {
out << "ExceptionRecord:\n";
out << "\tExceptionCode: " << er.ExceptionCode << endl;
out << "\tExceptionFlags: " << er.ExceptionFlags << endl;
out << "\t*ExceptionRecord: " << er.ExceptionRecord << endl;
out << "\tExceptionAddress: " << er.ExceptionAddress << endl;
out << "\tNumberParameters: " << er.NumberParameters << endl;
for(DWORD i = 0; i < er.NumberParameters; i++) {
out << "\tExceptionInformation[" << i << "] = " <<
er.ExceptionInformation[i] << endl;
}
if (er.ExceptionRecord) {
out << "\n\nPrinting next exception:\n";
PrintExceptionRecord(out, *(er.ExceptionRecord));
}
return out;
}
ostream &PrintException(ostream &out, const PEXCEPTION_POINTERS ptr) const {
if (!ptr) {
return out;
}
if (ptr->ExceptionRecord) {
PrintExceptionRecord(out, *(ptr->ExceptionRecord));
}
return out;
}
public:
CMyException()
: m_ErrorCode(0), m_pExp(NULL) {
}
CMyException(unsigned int i, EXCEPTION_POINTERS* pExp)
: m_ErrorCode(i), m_pExp(pExp) {
}
CMyException(CMyException &c)
: m_ErrorCode(c.m_ErrorCode), m_pExp(c.m_pExp) {
}
friend ostream &operator<<(ostream &out, const CMyException &inst) {
out << "CMyException:\n" << "Error Code: " << (hex) << inst.m_ErrorCode << endl;
return inst.PrintException(out, inst.m_pExp);
}
};
void TransFunc(unsigned int u, PEXCEPTION_POINTERS pExp)
{
cout << "In TransFunc.\n";
CMyException e(u, pExp);
throw e;
}
int main() {
//
// Catch a CLR exception as a C++ exception
//
_set_se_translator(TransFunc);
cout << "Com+ exception code will be " << (hex) << (0xE0000000 | 'COM') << endl;
try {
thrower();
}
catch(CMyException e) {
cout << e << endl;
}
cout << "**DONE**" << endl;
return 0;
}