1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
11
// * Redistributions of source code must retain the above copyright
12
// notice, this list of conditions and the following disclaimer.
13
// * Redistributions in binary form must reproduce the above
14
// copyright notice, this list of conditions and the following disclaimer
15
// in the documentation and/or other materials provided with the
17
// * Neither the name of Industrial Light & Magic nor the names of
18
// its contributors may be used to endorse or promote products derived
19
// from this software without specific prior written permission.
21
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
///////////////////////////////////////////////////////////////////////////
37
#ifndef INCLUDED_IEXBASEEXC_H
38
#define INCLUDED_IEXBASEEXC_H
41
//----------------------------------------------------------
43
// A general exception base class, and a few
44
// useful exceptions derived from the base class.
46
//----------------------------------------------------------
54
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
55
// Tell MS VC++ to suppress exception specification warnings
56
#pragma warning(disable:4290)
59
//-------------------------------
60
// Our most basic exception class
61
//-------------------------------
63
class BaseExc: public std::string, public std::exception
67
//----------------------------
68
// Constructors and destructor
69
//----------------------------
71
BaseExc (const char *s = 0) throw(); // std::string (s)
72
BaseExc (const std::string &s) throw(); // std::string (s)
73
BaseExc (std::stringstream &s) throw(); // std::string (s.str())
75
BaseExc (const BaseExc &be) throw();
76
virtual ~BaseExc () throw ();
78
//--------------------------------------------
79
// what() method -- e.what() returns e.c_str()
80
//--------------------------------------------
82
virtual const char * what () const throw ();
85
//--------------------------------------------------
86
// Convenient methods to change the exception's text
87
//--------------------------------------------------
89
BaseExc & assign (std::stringstream &s); // assign (s.str())
90
BaseExc & operator = (std::stringstream &s);
92
BaseExc & append (std::stringstream &s); // append (s.str())
93
BaseExc & operator += (std::stringstream &s);
96
//--------------------------------------------------
97
// These methods from the base class get obscured by
98
// the definitions above.
99
//--------------------------------------------------
101
BaseExc & assign (const char *s);
102
BaseExc & operator = (const char *s);
104
BaseExc & append (const char *s);
105
BaseExc & operator += (const char *s);
108
//--------------------------------------------------
109
// Stack trace for the point at which the exception
110
// was thrown. The stack trace will be an empty
111
// string unless a working stack-tracing routine
112
// has been installed (see below, setStackTracer()).
113
//--------------------------------------------------
115
const std::string & stackTrace () const;
119
std::string _stackTrace;
123
//-----------------------------------------------------
124
// A macro to save typing when declararing an exception
125
// class derived directly or indirectly from BaseExc:
126
//-----------------------------------------------------
128
#define DEFINE_EXC(name, base) \
129
class name: public base \
132
name (const char* text=0) throw(): base (text) {} \
133
name (const std::string &text) throw(): base (text) {} \
134
name (std::stringstream &text) throw(): base (text) {} \
138
//--------------------------------------------------------
139
// Some exceptions which should be useful in most programs
140
//--------------------------------------------------------
142
DEFINE_EXC (ArgExc, BaseExc) // Invalid arguments to a function call
144
DEFINE_EXC (LogicExc, BaseExc) // General error in a program's logic,
145
// for example, a function was called
146
// in a context where the call does
149
DEFINE_EXC (InputExc, BaseExc) // Invalid input data, e.g. from a file
151
DEFINE_EXC (IoExc, BaseExc) // Input or output operation failed
153
DEFINE_EXC (MathExc, BaseExc) // Arithmetic exception; more specific
154
// exceptions derived from this class
155
// are defined in ExcMath.h
157
DEFINE_EXC (ErrnoExc, BaseExc) // Base class for exceptions corresponding
158
// to errno values (see errno.h); more
159
// specific exceptions derived from this
160
// class are defined in ExcErrno.h
162
DEFINE_EXC (NoImplExc, BaseExc) // Missing method exception e.g. from a
163
// call to a method that is only partially
164
// or not at all implemented. A reminder
165
// to lazy software people to get back
168
DEFINE_EXC (NullExc, BaseExc) // A pointer is inappropriately null.
170
DEFINE_EXC (TypeExc, BaseExc) // An object is an inappropriate type,
171
// i.e. a dynamnic_cast failed.
174
//----------------------------------------------------------------------
175
// Stack-tracing support:
177
// setStackTracer(st)
179
// installs a stack-tracing routine, st, which will be called from
180
// class BaseExc's constructor every time an exception derived from
181
// BaseExc is thrown. The stack-tracing routine should return a
182
// string that contains a printable representation of the program's
183
// current call stack. This string will be stored in the BaseExc
184
// object; the string is accesible via the BaseExc::stackTrace()
189
// removes the current stack tracing routine. When an exception
190
// derived from BaseExc is thrown, the stack trace string stored
191
// in the BaseExc object will be empty.
195
// returns a pointer to the current stack-tracing routine, or 0
196
// if there is no current stack stack-tracing routine.
198
//----------------------------------------------------------------------
200
typedef std::string (* StackTracer) ();
202
void setStackTracer (StackTracer stackTracer);
203
StackTracer stackTracer ();
211
BaseExc::operator = (std::stringstream &s)
218
BaseExc::operator += (std::stringstream &s)
225
BaseExc::assign (const char *s)
227
std::string::assign(s);
233
BaseExc::operator = (const char *s)
240
BaseExc::append (const char *s)
242
std::string::append(s);
248
BaseExc::operator += (const char *s)
254
inline const std::string &
255
BaseExc::stackTrace () const
260
#if (defined _WIN32 || defined _WIN64) && defined _MSC_VER
261
#pragma warning(default:4290)