2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 //---------------------------------------------------------
12 // This include should be moved to commons but even as it is,
13 // it wont cause a linker circular dependency since it's just
15 #include "utils/StringUtils.h"
16 //---------------------------------------------------------
21 // The 'this' pointer counts as a parameter on member methods.
22 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT __attribute__((format(printf,2,3)))
24 #define XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
27 #define XBMCCOMMONS_COPYVARARGS(fmt) va_list argList; va_start(argList, fmt); Set(fmt, argList); va_end(argList)
28 #define XBMCCOMMONS_STANDARD_EXCEPTION(E) \
29 class E : public XbmcCommons::Exception \
32 inline E(const char* message,...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT : Exception(#E) { XBMCCOMMONS_COPYVARARGS(message); } \
34 inline E(const E& other) : Exception(other) {} \
40 * This class a superclass for exceptions that want to utilize some
41 * utility functionality including autologging with the specific
48 std::string classname
;
53 inline explicit Exception(const char* classname_
) : classname(classname_
) { }
54 inline Exception(const char* classname_
, const char* message_
) : classname(classname_
), message(message_
) { }
55 inline Exception(const Exception
& other
) = default;
58 * This method is called from the constructor of subclasses. It
59 * will set the message from varargs as well as call log message
61 inline void Set(const char* fmt
, va_list& argList
)
63 message
= StringUtils::FormatV(fmt
, argList
);
67 * This message can be called from the constructor of subclasses.
68 * It will set the message and log the throwing.
70 inline void SetMessage(const char* fmt
, ...) XBMCCOMMONS_ATTRIB_EXCEPTION_FORMAT
73 XBMCCOMMONS_COPYVARARGS(fmt
);
76 inline void setClassname(const char* cn
) { classname
= cn
; }
81 virtual void LogThrowMessage(const char* prefix
= NULL
) const;
83 inline virtual const char* GetExMessage() const { return message
.c_str(); }
87 * This class forms the base class for unchecked exceptions. Unchecked exceptions
88 * are those that really shouldn't be handled explicitly. For example, on windows
89 * when a access violation is converted to a win32_exception, there's nothing
90 * that can be done in most code. The outer most stack frame might try to
91 * do some error logging prior to shutting down, but that's really it.
93 XBMCCOMMONS_STANDARD_EXCEPTION(UncheckedException
);
96 * In cases where you catch(...){} you will (may) inadvertently be
97 * catching UncheckedException's. Therefore this macro will allow
98 * you to do something equivalent to:
99 * catch (anything except UncheckedException) {}
101 * In order to avoid catching UncheckedException, use the macro as follows:
104 * XBMCCOMMONS_HANDLE_UNCHECKED
107 // Yes. I recognize that the name of this macro is an oxymoron.
108 #define XBMCCOMMONS_HANDLE_UNCHECKED \
109 catch (const XbmcCommons::UncheckedException& ) { throw; } \
110 catch (const XbmcCommons::UncheckedException* ) { throw; }