1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/************************************************************************
$Id: initexceptions.cpp,v 1.2 2005/01/06 17:59:18 jonico Exp $
RTB - Team Framework: Framework for RealTime Battle robots to communicate efficiently in a team
Copyright (C) 2004 The RTB- Team Framework Group: http://rtb-team.sourceforge.net
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Log: initexceptions.cpp,v $
Revision 1.2 2005/01/06 17:59:18 jonico
Now all files in the repository have their new header format.
**************************************************************************/
#include <common.h>
#include <exception>
#include <string>
#include "rtbglobal/masterresourcecontrol.h"
#include <stdlib.h>
/**
* NOTE: These file contains the uncaught exception handler and the unexpected exception handler
* DO NOT FORGET TO LINK THE CORRESPONDING OBJECT CODE TO THE RTB - BINARY IF YOU WANT PROPER EXCEPTION HANDLING
*/
namespace { // anonymous namespace, all things in here are solely for exception handling purposes
using std::bad_exception;
using std::exception;
using std::string;
using RTBGlobal::MasterResourceControl;
class __Extended_bad_exception: public bad_exception { // the origninal bad_exception is not capable of saving information about the unexpected exception, this one solves this issue
public:
__Extended_bad_exception(const string& ex):reason("bad_exception: "+ex) {};
virtual const char* what() const throw () { return reason.c_str();};
virtual ~__Extended_bad_exception() throw() {};
private:
string reason;
};
// global unexpected exception handler
void rtb_unexpected_exception_handler() throw (bad_exception)
{
// try to figure out what type of unexpected exception occured
try {
throw;
}
catch (std::exception& ex) {
// we were able to identify the unexpected exception
throw __Extended_bad_exception("An unexpected (identified) exception has appeared: "+string(ex.what()));
}
catch (...) {
throw __Extended_bad_exception(string("An unexpected (unidentified) exception has appeared!"));
}
}
// global uncaught exception handler
void rtb_uncaught_exception_handler() throw()
{
// try to figure out what type of exception occured
try {
throw;
}
catch (std::exception& ex) {
// we were able to identify the unexpected exception
MasterResourceControl::EmergencyLog("An uncaught (identified) exception has appeared: "+string(ex.what()));
abort();
}
catch (...) {
MasterResourceControl::EmergencyLog(string("An uncaught (unidentified) exception has appeared!"));
abort();
}
}
class __RTBHandleSetter { // class to set the exception handler callback methods
public:
__RTBHandleSetter() {
std::set_unexpected(&rtb_unexpected_exception_handler);
std::set_terminate(&rtb_uncaught_exception_handler);
}
~__RTBHandleSetter() throw() {};
};
__RTBHandleSetter __initialHandleSetter; // tries to set exception handler even before main method will start
}
|