From f158dbf8a2a27f7cb791af571d461b5b4cb76851 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 15 Aug 2020 14:50:17 +0200 Subject: [PATCH] Throw all exceptions with Arbutils exception. --- src/Assert.hpp | 3 ++- src/Collections/Dictionary.hpp | 2 +- src/Collections/List.hpp | 7 ++++--- src/Exception.hpp | 8 ++++---- src/Memory/UniquePtrList.hpp | 4 ++-- tests/AssertTests.cpp | 10 ++++++++-- 6 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Assert.hpp b/src/Assert.hpp index 5af481f..7cf53b2 100644 --- a/src/Assert.hpp +++ b/src/Assert.hpp @@ -1,6 +1,7 @@ #include #include #include +#include "Exception.hpp" #define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) @@ -10,7 +11,7 @@ std::stringstream ss; \ ss << "ASSERTION FAILED: [" << __FILENAME__ << " (" << __LINE__ << ")] \""; \ ss << #expr << "\""; \ - throw std::logic_error(ss.str()); \ + throw ArbUt::Exception(ss.str()); \ } #define AssertForEach(iterator, assertion) \ { \ diff --git a/src/Collections/Dictionary.hpp b/src/Collections/Dictionary.hpp index 94a6f51..c999f7e 100644 --- a/src/Collections/Dictionary.hpp +++ b/src/Collections/Dictionary.hpp @@ -23,7 +23,7 @@ namespace ArbUt { [[maybe_unused]] const auto& v = _map.insert({key, value}); #ifndef NO_ASSERT if (!v.second) - throw std::logic_error("Key already exists"); + throw ArbUt::Exception("Key already exists"); #endif } diff --git a/src/Collections/List.hpp b/src/Collections/List.hpp index 2c2262f..8a5a1b6 100644 --- a/src/Collections/List.hpp +++ b/src/Collections/List.hpp @@ -4,6 +4,7 @@ #include #include #include +#include "../Exception.hpp" namespace ArbUt { template class List { @@ -27,7 +28,7 @@ namespace ArbUt { if (index >= _vector.size() || index < 0) { std::stringstream ss; ss << "Index " << index << " is out of bounds."; - throw std::logic_error(ss.str()); + throw ArbUt::Exception(ss.str()); } #endif return _vector.at(index); @@ -38,7 +39,7 @@ namespace ArbUt { if (index >= _vector.size() || index < 0) { std::stringstream ss; ss << "Index " << index << " is out of bounds."; - throw std::logic_error(ss.str()); + throw ArbUt::Exception(ss.str()); } #endif return _vector.at(index); @@ -49,7 +50,7 @@ namespace ArbUt { if (index >= _vector.size() || index < 0) { std::stringstream ss; ss << "Index " << index << " is out of bounds."; - throw std::logic_error(ss.str()); + throw ArbUt::Exception(ss.str()); } #endif const auto& prev = _vector.at(index); diff --git a/src/Exception.hpp b/src/Exception.hpp index 564c00d..154c1a2 100644 --- a/src/Exception.hpp +++ b/src/Exception.hpp @@ -11,20 +11,20 @@ #endif namespace ArbUt { - class Exception : std::exception { - std::string _msg; + class Exception : std::logic_error { #if !WINDOWS backward::StackTrace _stack; #endif public: - explicit Exception(std::string msg) : _msg(std::move(msg)) { + explicit Exception(const std::string& msg) : std::logic_error(msg) { #if !WINDOWS _stack.load_here(9); #endif } - [[nodiscard]] const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg.c_str(); } + public: + const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return logic_error::what(); } [[nodiscard]] std::string GetStacktrace([[maybe_unused]] size_t depth = 6, [[maybe_unused]] bool include_addr = true) const { diff --git a/src/Memory/UniquePtrList.hpp b/src/Memory/UniquePtrList.hpp index 9ea8e46..55da9d2 100644 --- a/src/Memory/UniquePtrList.hpp +++ b/src/Memory/UniquePtrList.hpp @@ -36,7 +36,7 @@ namespace ArbUt { if (index >= _vector.size()) { std::stringstream ss; ss << "Index " << index << " is out of bounds."; - throw std::logic_error(ss.str()); + throw ArbUt::Exception(ss.str()); } #endif return _vector[index]; @@ -61,7 +61,7 @@ namespace ArbUt { if (index >= _vector.size()) { std::stringstream ss; ss << "Index " << index << " is out of bounds."; - throw std::logic_error(ss.str()); + throw ArbUt::Exception(ss.str()); } #endif delete _vector[index]; diff --git a/tests/AssertTests.cpp b/tests/AssertTests.cpp index 7d627ab..9614ce2 100644 --- a/tests/AssertTests.cpp +++ b/tests/AssertTests.cpp @@ -8,8 +8,14 @@ TEST_CASE("Assert succeeds if true", "[Utilities]") { REQUIRE_NOTHROW(TestWrappe TEST_CASE("Assert throws if false", "[Utilities]") { REQUIRE_THROWS(TestWrapper(false)); } TEST_CASE("Assert throws if false with message", "[Utilities]") { - REQUIRE_THROWS_WITH(TestWrapper(false), - Catch::Matchers::Equals("ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\"")); + try { + TestWrapper(false); + } + catch (const ArbUt::Exception& e) { + REQUIRE(strcmp(e.what(), "ASSERTION FAILED: [AssertTests.cpp (3)] \"wrapperExpression\"") == 0); + return; + } + throw ArbUt::Exception("Didn't throw."); } TEST_CASE("Multiple asserts", "[Utilities]") {