Exception.h

00001 #ifndef EXCEPTION_H
00002 #define EXCEPTION_H
00003 
00004 // --8<--8<--8<--8<--
00005 //
00006 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00007 //
00008 // This file is part of Exception
00009 //
00010 // Exception is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU General Public License
00012 // as published by the Free Software Foundation; either version 2
00013 // of the License, or (at your option) any later version.
00014 //
00015 // Exception is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU General Public License
00021 // along with this program; if not, write to the 
00022 //       Free Software Foundation, Inc. 
00023 //       51 Franklin Street, Fifth Floor
00024 //       Boston, MA  02110-1301, USA
00025 //
00026 // -->8-->8-->8-->8--
00027 
00028 #include <stdarg.h>
00029 #include <iostream>
00030 
00031 #ifdef sgi
00032 #  include <stdio.h>
00033 #else
00034 #  include <cstdio>
00035 #endif
00036 
00037 #include <string>
00038 #include <stdexcept>
00039 #include <deque>
00040 
00041 using namespace std;
00042 
00049 class Exception : public runtime_error {
00050 
00055   friend ostream& operator << ( ostream& os, Exception& a ) {
00056     a.print( os );
00057     return os;
00058   }
00059 
00064   friend ostream& operator << ( ostream& os, Exception* a ) {
00065     os << *a;
00066     return os;
00067   }
00068 
00069 public:
00070 
00071   // GCC (and Stroustrup) require a virtual function may be overridden */
00072   // only by a function that has an exception-specification AT LEAST
00073   // as restrictive as its own...
00074   virtual ~Exception( ) throw( ) { }
00075 
00080   Exception( ) : runtime_error( "" ) { }
00081 
00082   Exception( const Exception& e ) : runtime_error( e.what( ) ) {
00083     //cerr << "The copy constructor Exception( const Exception& ) was called\n";
00084     this->operator=( e );
00085   }
00086 
00090   Exception( const string& arg ) : runtime_error( arg ) { 
00091     // cerr << "Exception( const string& ) was called\n";
00092     set_message( arg );
00093   }
00094 
00095   Exception& operator= ( const Exception& rhs );
00096 
00100   deque<string>::const_iterator begin( ) {
00101     return exception_queue.begin( );
00102   }
00103 
00107   deque<string>::const_iterator end( ) {
00108     return exception_queue.end( );
00109   }
00110 
00114   string get_message( void ) const {
00115     return exception_queue[ 0 ];
00116   }
00117 
00121   void set_message( const string& msg );
00122 
00123   void set_rethrow_message( const string& file, const int linenum );
00124 
00125   const char* what() const throw();
00126 
00127 protected:
00128 
00129   deque<string> exception_queue;
00130 
00134   virtual void print( ostream& os=cerr ) const;
00135 
00136 };
00137 
00138 #define RETHROWME( arg ) {arg.set_rethrow_message( __FILE__, __LINE__ ); throw arg;}
00139 
00145 #endif