/
exceptions.cc
140 lines (114 loc) · 3.27 KB
/
exceptions.cc
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
// Copyright (c) 2001-2011 XORP, Inc and Others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License, Version
// 2.1, June 1999 as published by the Free Software Foundation.
// Redistribution and/or modification of this program under the terms of
// any other version of the GNU Lesser General Public License is not
// permitted.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
// see the GNU Lesser General Public License, Version 2.1, a copy of
// which can be found in the XORP LICENSE.lgpl file.
//
// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
// http://xorp.net
#include "exceptions.hh"
#include <stdarg.h>
#include <stdio.h>
#ifndef XORP_USE_USTL
#include <typeinfo>
#endif
XorpException::XorpException(const char* init_what,
const char* file,
size_t line)
: _what(init_what), _file(file), _line(line)
{
}
XorpException::~XorpException()
{
}
const string
XorpException::where() const
{
return c_format("line %u of %s", XORP_UINT_CAST(_line), _file);
}
const string
XorpException::why() const
{
return "Not specified";
}
string
XorpException::str() const
{
return what() + " from " + where() + ": " + why();
}
XorpReasonedException::XorpReasonedException(const char* init_what,
const char* file,
size_t line,
const string& init_why)
: XorpException(init_what, file, line), _why(init_why)
{
}
const string
XorpReasonedException::why() const
{
return ( _why.size() != 0 ) ? _why : string("Not specified");
}
InvalidBufferOffset::InvalidBufferOffset(const char* file,
size_t line,
const string& init_why)
: XorpReasonedException("XorpInvalidBufferOffset", file, line, init_why)
{
}
InvalidPacket::InvalidPacket(const char* file,
size_t line,
const string& init_why)
: XorpReasonedException("XorpInvalidPacket", file, line, init_why)
{
}
// ----------------------------------------------------------------------------
// Handlers
void
xorp_catch_standard_exceptions() {
xorp_print_standard_exceptions();
terminate();
}
void
xorp_print_standard_exceptions() {
try {
throw; // Re-throw so we can inspect exception type
} catch (const XorpException& xe) {
cerr << xe.what() << " from " << xe.where() << " -> "
<< xe.why() << "\n";
} catch (const exception& e) {
cerr << "Standard exception: "
<< e.what() << " (name = \"" << typeid(e).name() << "\")\n";
}
}
void
xorp_unexpected_handler(void) {
cerr << "Unexpected exception: "
<< "\tthrown did not correspond to specification - fix code.\n";
xorp_catch_standard_exceptions();
}
// ----------------------------------------------------------------------------
// EXAMPLE
//#define XORP_EXAMPLE_USAGE
#ifdef XORP_EXAMPLE_USAGE
#include <bitset>
int main() {
XorpUnexpectedHandler x(xorp_unexpected_handler);
try {
bitset<8> bs;
bs.set(1000); // will throw out_of_range("bitset");
foo();
} catch (...) {
xorp_catch_standard_exceptions();
}
return 0;
}
#endif