1 #ifndef __CORE_EXCEPTION_H__ 2 #define __CORE_EXCEPTION_H__ 3 4 /* M-runtime for c++ 5 * Copyright (C) 2005-2008 Vladimir Menshakov 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21 22 #include <stdio.h> 23 #include <string> 24 #include <exception> 25 #include "fmt.h" 26 #include "logger.h" 27 #include "export_mrt.h" 28 29 namespace mrt { 30 31 class MRTAPI Exception : public std::exception { 32 public: 33 Exception(); 34 void add_message(const char *file, const int line); 35 void add_message(const std::string &msg); 36 virtual const std::string get_custom_message(); 37 virtual const char* what() const throw(); 38 virtual ~Exception() throw(); 39 private: 40 std::string _error; 41 }; 42 43 } 44 45 #define DERIVE_EXCEPTION(export, name) \ 46 class export name : public mrt::Exception { \ 47 public: \ 48 name(); \ 49 const std::string get_custom_message(); \ 50 virtual ~name() throw(); \ 51 } 52 53 #define DERIVE_EXCEPTION_NO_DEFAULT(export, name, ctor, data) \ 54 class export name : public mrt::Exception { \ 55 public: \ 56 name ctor; \ 57 const std::string get_custom_message(); \ 58 virtual ~name() throw(); \ 59 private: \ 60 data \ 61 } 62 63 #define throw_generic(name, str) { name e; e.add_message(__FILE__, __LINE__); e.add_message(mrt::format_string str); e.add_message(e.get_custom_message()); throw e; } 64 #define throw_generic_no_default(name, str, args) { name e args; e.add_message(__FILE__, __LINE__); e.add_message(mrt::format_string str); e.add_message(e.get_custom_message()); throw e; } 65 #define throw_ex(str) throw_generic(mrt::Exception, str) 66 67 #define TRY try 68 69 #if defined _WINDOWS && defined DEBUG 70 # define CATCH_D(where, code) /* bye bye */ 71 #else 72 # define CATCH_D(where, code) catch(...) {\ 73 LOG_ERROR(("%s: unknown error", where));\ 74 code;\ 75 } 76 #endif 77 78 #define CATCH(where, code) catch(const char * _e) {\ 79 LOG_ERROR(("%s: (const char*) %s", where, _e)); \ 80 code;\ 81 } catch(const std::exception &_e) {\ 82 LOG_ERROR(("%s: %s", where, _e.what())); \ 83 code;\ 84 } 85 86 #endif 87