Skip to content

Commit

Permalink
use a macro to determine whether except::Throwable derives from std::…
Browse files Browse the repository at this point in the history
…exception
  • Loading branch information
Dan Smith committed Dec 28, 2022
1 parent 49deb0f commit 5210a3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 4 additions & 6 deletions modules/c++/except/include/except/Exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#endif
#endif

#define DECLARE_EXTENDED_EXCEPTION_(_Name, Exception_, _Base, getType_specifiers) \
#define DECLARE_EXTENDED_EXCEPTION_(_Name, Exception_, _Base) \
struct _Name##Exception_ : public _Base \
{ \
_Name##Exception_() = default; virtual ~_Name##Exception_() = default; \
Expand All @@ -62,12 +62,10 @@
_Name##Exception_(const except::Throwable& t, const except::Context& c) : _Base(t, c){} \
_Name##Exception_(const except::Throwable11& t, const except::Context& c) : _Base(t, c){} \
CODA_OSS_except_Exception_suppress_26447_BEGIN_ \
std::string getType() getType_specifiers { return #_Name #Exception_; } \
std::string getType() const noexcept override { return #_Name #Exception_; } \
CODA_OSS_except_Exception_suppress_26447_END_ };
#define DECLARE_EXTENDED_EXCEPTION(_Name, _Base) \
DECLARE_EXTENDED_EXCEPTION_(_Name, Exception, _Base, const override)
#define DECLARE_EXTENDED_EXCEPTION11(_Name, _Base) \
DECLARE_EXTENDED_EXCEPTION_(_Name, Exception11, _Base, const noexcept override)
#define DECLARE_EXTENDED_EXCEPTION(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, Exception, _Base)
#define DECLARE_EXTENDED_EXCEPTION11(_Name, _Base) DECLARE_EXTENDED_EXCEPTION_(_Name, Exception11, _Base)

#define DECLARE_EXCEPTION(_Name) \
DECLARE_EXTENDED_EXCEPTION(_Name, except::Exception) \
Expand Down
24 changes: 24 additions & 0 deletions modules/c++/except/include/except/Throwable.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@
#include "config/compiler_extensions.h"
#include "except/Trace.h"

/* Determine whether except::Throwable derives from std::exception.
*
* It can be quite convenient to derive from std::exception as often one less
* "catch" will be needed and we'll have standard what(). But doing so could
* break existing code as "catch (const std::exception&)" will catch
* except::Throwable when it didn't before.
*/
#ifdef CODA_OSS_THROWABLE_IS_NOT_AN_EXCEPTION // -DCODA_OSS_THROWABLE_IS_NOT_AN_EXCEPTION
#ifdef CODA_OSS_except_Throwable_ISA_std_exception
#error "CODA_OSS_except_Throwable_ISA_std_exception already #define'd."
#endif
#define CODA_OSS_except_Throwable_ISA_std_exception 0
#endif
#ifndef CODA_OSS_except_Throwable_ISA_std_exception // or, -DCODA_OSS_except_Throwable_ISA_std_exception=0
#define CODA_OSS_except_Throwable_ISA_std_exception 0
#endif

/*!
* \file Throwable.h
* \brief Contains the classes to do with error handling
Expand All @@ -55,6 +72,9 @@ namespace except

class Throwable11;
class CODA_OSS_API Throwable
#if CODA_OSS_except_Throwable_ISA_std_exception
: public std::exception
#endif
{
void doGetBacktrace();
template<typename TThrowable>
Expand Down Expand Up @@ -169,6 +189,10 @@ class CODA_OSS_API Throwable
}

const char* what() const noexcept
#if CODA_OSS_except_Throwable_ISA_std_exception
// can't use "final" unless what() is virtual
final // derived classes override toString()
#endif
{
// adding this to toString() output could (significantly) alter existing display
mWhat = toString(true /*includeBacktrace*/); // call any derived toString()
Expand Down
10 changes: 5 additions & 5 deletions modules/c++/sys/tests/MemoryTest1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ int main(int, char**)
std::cout << "Total system memory: " << totalPhysMem << " MB" << std::endl;
std::cout << "Free system memory: " << freePhysMem << " MB" << std::endl;
}
catch (const std::exception& ex)
{
std::cerr << "Caught std::exception: " << ex.what() << std::endl;
return 1;
}
catch (const except::Exception& ex)
{
std::cerr << "Caught except::exception: " << ex.getMessage()
<< std::endl;
return 1;
}
catch (const std::exception& ex)
{
std::cerr << "Caught std::exception: " << ex.what() << std::endl;
return 1;
}
catch(...)
{
std::cerr << "Caught unknown exception\n";
Expand Down

0 comments on commit 5210a3a

Please sign in to comment.