1 // exception.cxx - implementation of SimGear base exceptions.
2 // Started Summer 2001 by David Megginson, david@megginson.com
3 // This code is released into the Public Domain.
8 #include "exception.hxx"
15 ////////////////////////////////////////////////////////////////////////
16 // Implementation of sg_location class.
17 ////////////////////////////////////////////////////////////////////////
19 sg_location::sg_location ()
27 sg_location::sg_location (const string& path, int line, int column)
32 setPath(path.c_str());
35 sg_location::sg_location (const char* path, int line, int column)
43 sg_location::~sg_location () throw ()
48 sg_location::getPath () const
54 sg_location::setPath (const char* path)
57 strncpy(_path, path, max_path);
58 _path[max_path -1] = '\0';
65 sg_location::getLine () const
71 sg_location::setLine (int line)
77 sg_location::getColumn () const
83 sg_location::setColumn (int column)
89 sg_location::getByte () const
95 sg_location::setByte (int byte)
101 sg_location::asString () const
103 std::ostringstream out;
106 if (_line != -1 || _column != -1)
110 out << "line " << _line;
115 out << "column " << _column;
122 ////////////////////////////////////////////////////////////////////////
123 // Implementation of sg_throwable class.
124 ////////////////////////////////////////////////////////////////////////
126 sg_throwable::sg_throwable ()
132 sg_throwable::sg_throwable (const char* message, const char* origin)
138 sg_throwable::~sg_throwable () throw ()
143 sg_throwable::getMessage () const
149 sg_throwable::getFormattedMessage () const
151 return string(getMessage());
155 sg_throwable::setMessage (const char* message)
157 strncpy(_message, message, MAX_TEXT_LEN);
158 _message[MAX_TEXT_LEN - 1] = '\0';
163 sg_throwable::getOrigin () const
169 sg_throwable::setOrigin (const char* origin)
172 strncpy(_origin, origin, MAX_TEXT_LEN);
173 _origin[MAX_TEXT_LEN - 1] = '\0';
179 const char* sg_throwable::what() const throw()
190 ////////////////////////////////////////////////////////////////////////
191 // Implementation of sg_error class.
192 ////////////////////////////////////////////////////////////////////////
194 sg_error::sg_error ()
199 sg_error::sg_error (const char* message, const char *origin)
200 : sg_throwable(message, origin)
204 sg_error::sg_error (const string& message, const string& origin)
205 : sg_throwable(message.c_str(), origin.c_str())
209 sg_error::~sg_error () throw ()
213 ////////////////////////////////////////////////////////////////////////
214 // Implementation of sg_exception class.
215 ////////////////////////////////////////////////////////////////////////
217 sg_exception::sg_exception ()
222 sg_exception::sg_exception (const char* message, const char* origin)
223 : sg_throwable(message, origin)
227 sg_exception::sg_exception (const string& message, const string& origin)
228 : sg_throwable(message.c_str(), origin.c_str())
232 sg_exception::~sg_exception () throw ()
236 ////////////////////////////////////////////////////////////////////////
237 // Implementation of sg_io_exception.
238 ////////////////////////////////////////////////////////////////////////
240 sg_io_exception::sg_io_exception ()
245 sg_io_exception::sg_io_exception (const char* message, const char* origin)
246 : sg_exception(message, origin)
250 sg_io_exception::sg_io_exception (const char* message,
251 const sg_location &location,
253 : sg_exception(message, origin),
258 sg_io_exception::sg_io_exception (const string& message, const string& origin)
259 : sg_exception(message, origin)
263 sg_io_exception::sg_io_exception (const string& message,
264 const sg_location &location,
265 const string& origin)
266 : sg_exception(message, origin),
271 sg_io_exception::~sg_io_exception () throw ()
276 sg_io_exception::getFormattedMessage () const
278 string ret = getMessage();
279 string loc = getLocation().asString();
288 sg_io_exception::getLocation () const
294 sg_io_exception::setLocation (const sg_location &location)
296 _location = location;
301 ////////////////////////////////////////////////////////////////////////
302 // Implementation of sg_format_exception.
303 ////////////////////////////////////////////////////////////////////////
305 sg_format_exception::sg_format_exception ()
311 sg_format_exception::sg_format_exception (const char* message,
314 : sg_exception(message, origin)
319 sg_format_exception::sg_format_exception (const string& message,
321 const string& origin)
322 : sg_exception(message, origin)
324 setText(text.c_str());
327 sg_format_exception::~sg_format_exception () throw ()
332 sg_format_exception::getText () const
338 sg_format_exception::setText (const char* text)
341 strncpy(_text, text, MAX_TEXT_LEN);
342 _text[MAX_TEXT_LEN-1] = '\0';
350 ////////////////////////////////////////////////////////////////////////
351 // Implementation of sg_range_exception.
352 ////////////////////////////////////////////////////////////////////////
354 sg_range_exception::sg_range_exception ()
359 sg_range_exception::sg_range_exception (const char* message,
361 : sg_exception(message, origin)
365 sg_range_exception::sg_range_exception(const string& message,
366 const string& origin)
367 : sg_exception(message, origin)
371 sg_range_exception::~sg_range_exception () throw ()
374 // end of exception.cxx