2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
23 //---------------------------------------------------------
24 // This include should be moved to commons but even as it is,
25 // it wont cause a linker circular dependency since it's just
27 #include "utils/StdString.h"
28 //---------------------------------------------------------
32 // The 'this' pointer counts as a parameter on member methods.
33 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT __attribute__((format(printf,2,3)))
35 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
38 #define XBMCCOMMONS_COPYVARARGS(fmt) va_list argList; va_start(argList, fmt); Set(fmt, argList); va_end(argList)
39 #define XBMCCOMMONS_STANDARD_EXCEPTION(E) \
40 class E : public XbmcCommons::Exception \
43 inline E(const char* message,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT : Exception(#E) { XBMCCOMMONS_COPYVARARGS(message); } \
45 inline E(const E& other) : Exception(other) {} \
51 * This class a superclass for exceptions that want to utilize some
52 * utility functionality including autologging with the specific
59 std::string classname;
63 static ILogger* logger;
65 inline Exception(const char* classname_) : classname(classname_) { }
66 inline Exception(const char* classname_, const char* message_) : classname(classname_), message(message_) { }
67 inline Exception(const Exception& other) : classname(other.classname), message(other.message) { }
70 * This method is called from the constructor of subclasses. It
71 * will set the message from varargs as well as call log message
73 inline void Set(const char* fmt, va_list& argList)
75 message.FormatV(fmt, argList);
79 * This message can be called from the constructor of subclasses.
80 * It will set the message and log the throwing.
82 inline void SetMessage(const char* fmt, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
85 XBMCCOMMONS_COPYVARARGS(fmt);
88 inline void setClassname(const char* cn) { classname = cn; }
93 inline virtual void LogThrowMessage(const char* prefix = NULL) const
96 logger->Log(LOGERROR,"EXCEPTION Thrown (%s) : %s", classname.c_str(), message.c_str());
99 inline virtual const char* GetMessage() const { return message.c_str(); }
101 inline static void SetLogger(ILogger* exceptionLogger) { logger = exceptionLogger; }
105 * This class forms the base class for unchecked exceptions. Unchecked exceptions
106 * are those that really shouldn't be handled explicitly. For example, on windows
107 * when a access violaton is converted to a win32_exception, there's nothing
108 * that can be done in most code. The outer most stack frame might try to
109 * do some error logging prior to shutting down, but that's really it.
111 XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException);
114 * In cases where you catch(...){} you will (may) inadvertently be
115 * catching UncheckedException's. Therefore this macro will allow
116 * you to do something equivalent to:
117 * catch (anything except UncheckedException) {}
119 * In order to avoid catching UncheckedException, use the macro as follows:
122 * XBMCCOMMONS_HANDLE_UNCHECKED
125 // Yes. I recognize that the name of this macro is an oxymoron.
126 #define XBMCCOMMONS_HANDLE_UNCHECKED \
127 catch (const XbmcCommons::UncheckedException& ) { throw; } \
128 catch (const XbmcCommons::UncheckedException* ) { throw; }