Exception.cc

00001 // --8<--8<--8<--8<--
00002 //
00003 // Copyright (C) 2006 Smithsonian Astrophysical Observatory
00004 //
00005 // This file is part of Exception
00006 //
00007 // Exception is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // Exception is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the 
00019 //       Free Software Foundation, Inc. 
00020 //       51 Franklin Street, Fifth Floor
00021 //       Boston, MA  02110-1301, USA
00022 //
00023 // -->8-->8-->8-->8--
00024 
00025 #include <sstream>
00026 
00027 #include <errno.h>
00028 
00029 #include "Exception.h"
00030 
00031 Exception& Exception::operator= ( const Exception& rhs ) {
00032 
00033   if ( this == &rhs )
00034     return *this;
00035 
00036   this->runtime_error::operator=( rhs );
00037   exception_queue = rhs.exception_queue;
00038 
00039   return *this;
00040 
00041 }
00042 
00043 void Exception::print( ostream& os ) const {
00044   for ( size_t i = 0; i < exception_queue.size( ); i++ )
00045     os << exception_queue[ i ];
00046 }
00047 
00048 void Exception::set_message( const string& msg ) {
00049   exception_queue.push_front( msg );
00050 }
00051 
00052 void Exception::set_rethrow_message( const string& file, const int n ) {
00053   ostringstream ost;
00054   ost << "Re-throw exception from file '" << file << "' at line " << n << '\n';
00055   exception_queue.push_front( ost.str( ) );
00056 }
00057 
00058 const char* Exception::what() const throw() {
00059 
00060   if ( exception_queue.size( ) )
00061     return exception_queue[ 0 ].c_str( );
00062   else
00063     return (const char*) NULL;
00064 
00065 }