Exception  1.0.7
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 #include <deque>
31 #include <string>
32 #include <stdexcept>
33 
40 class Exception : public std::runtime_error {
41 
46  friend std::ostream& operator << ( std::ostream& os, Exception& a ) {
47  a.print( os );
48  return os;
49  }
50 
55  friend std::ostream& operator << ( std::ostream& os, Exception* a ) {
56  os << *a;
57  return os;
58  }
59 
60 public:
61 
62  // GCC (and Stroustrup) require a virtual function may be overridden */
63  // only by a function that has an exception-specification AT LEAST
64  // as restrictive as its own...
65  virtual ~Exception( ) { }
66 
71  Exception( ) : runtime_error( "" ) { }
72 
73  Exception( const Exception& e ) : runtime_error( e.what( ) ) {
74  //cerr << "The copy constructor Exception( const Exception& ) was called\n";
75  this->operator=( e );
76  }
77 
81  Exception( const std::string& arg ) : runtime_error( arg ) {
82  // cerr << "Exception( const string& ) was called\n";
83  set_message( arg );
84  }
85 
86  Exception& operator= ( const Exception& rhs );
87 
91  std::deque<std::string>::const_iterator begin( ) const {
92  return exception_queue.begin( );
93  }
94 
98  std::deque<std::string>::const_iterator end( ) const {
99  return exception_queue.end( );
100  }
101 
105  std::string get_message( void ) const {
106  return exception_queue.empty() ? "" : exception_queue[ 0 ];
107  }
108 
112  void set_message( const std::string& msg );
113 
114  void set_rethrow_message( const std::string& file, const int linenum );
115 
116  const char* what() const noexcept;
117 
118  void update_what( );
119 
120 protected:
121 
122  std::deque<std::string> exception_queue;
123 
127  virtual void print( std::ostream& os=std::cerr ) const;
128 
129  std::string _what;
130 
131 };
132 
133 #define RETHROWME( arg ) {arg.set_rethrow_message( __FILE__, __LINE__ ); throw arg;}
134 
140 #endif
std::string get_message(void) const
Definition: Exception.h:105
friend std::ostream & operator<<(std::ostream &os, Exception &a)
Definition: Exception.h:46
Exception()
Definition: Exception.h:71
virtual void print(std::ostream &os=std::cerr) const
Definition: Exception.cc:46
Exception(const std::string &arg)
Definition: Exception.h:81
Definition: Exception.h:40
std::deque< std::string >::const_iterator end() const
Definition: Exception.h:98
void set_message(const std::string &msg)
Definition: Exception.cc:51
std::deque< std::string >::const_iterator begin() const
Definition: Exception.h:91