Exception  1.0.4
 All Classes Functions Friends
Exception.h
1 #ifndef EXCEPTION_H
2 #define EXCEPTION_H
3 
4 // --8<--8<--8<--8<--
5 //
6 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
7 //
8 // This file is part of Exception
9 //
10 // Exception is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License
12 // as published by the Free Software Foundation; either version 2
13 // of the License, or (at your option) any later version.
14 //
15 // Exception is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the
22 // Free Software Foundation, Inc.
23 // 51 Franklin Street, Fifth Floor
24 // Boston, MA 02110-1301, USA
25 //
26 // -->8-->8-->8-->8--
27 
28 #include <stdarg.h>
29 #include <iostream>
30 
31 #ifdef sgi
32 # include <stdio.h>
33 #else
34 # include <cstdio>
35 #endif
36 
37 #include <string>
38 #include <stdexcept>
39 #include <deque>
40 
41 using namespace std;
42 
49 class Exception : public runtime_error {
50 
55  friend ostream& operator << ( ostream& os, Exception& a ) {
56  a.print( os );
57  return os;
58  }
59 
64  friend ostream& operator << ( ostream& os, Exception* a ) {
65  os << *a;
66  return os;
67  }
68 
69 public:
70 
71  // GCC (and Stroustrup) require a virtual function may be overridden */
72  // only by a function that has an exception-specification AT LEAST
73  // as restrictive as its own...
74  virtual ~Exception( ) throw( ) { }
75 
80  Exception( ) : runtime_error( "" ) { }
81 
82  Exception( const Exception& e ) : runtime_error( e.what( ) ) {
83  //cerr << "The copy constructor Exception( const Exception& ) was called\n";
84  this->operator=( e );
85  }
86 
90  Exception( const string& arg ) : runtime_error( arg ) {
91  // cerr << "Exception( const string& ) was called\n";
92  set_message( arg );
93  }
94 
95  Exception& operator= ( const Exception& rhs );
96 
100  deque<string>::const_iterator begin( ) const {
101  return exception_queue.begin( );
102  }
103 
107  deque<string>::const_iterator end( ) const {
108  return exception_queue.end( );
109  }
110 
114  string get_message( void ) const {
115  return exception_queue[ 0 ];
116  }
117 
121  void set_message( const string& msg );
122 
123  void set_rethrow_message( const string& file, const int linenum );
124 
125  const char* what() const throw();
126 
127  void update_what( );
128 
129 protected:
130 
131  deque<string> exception_queue;
132 
136  virtual void print( ostream& os=cerr ) const;
137 
138  string _what;
139 
140 };
141 
142 #define RETHROWME( arg ) {arg.set_rethrow_message( __FILE__, __LINE__ ); throw arg;}
143 
149 #endif