From be6b2778a52b87b429fea9ed0036cc5c7eb20fc4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 22 Feb 2020 15:07:16 +0100 Subject: [PATCH] Make CreatureException inherit from runtime_error. --- src/Core/Exceptions/CreatureException.hpp | 10 +++------- tests/ExceptionTests.cpp | 5 ++++- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Core/Exceptions/CreatureException.hpp b/src/Core/Exceptions/CreatureException.hpp index a66dc51..8fc500a 100644 --- a/src/Core/Exceptions/CreatureException.hpp +++ b/src/Core/Exceptions/CreatureException.hpp @@ -1,16 +1,12 @@ #ifndef CREATURELIB_CREATUREEXCEPTION_HPP #define CREATURELIB_CREATUREEXCEPTION_HPP -#include +#include #include -class CreatureException : std::exception { - std::string _error; - +class CreatureException : public std::runtime_error { public: - explicit CreatureException(std::string error) : _error(error) {} - const char* what() const throw() override { return _error.c_str(); } - + explicit CreatureException(const std::string& error) : std::runtime_error(error) {} virtual ~CreatureException() = default; }; diff --git a/tests/ExceptionTests.cpp b/tests/ExceptionTests.cpp index 1c34155..c4f6612 100644 --- a/tests/ExceptionTests.cpp +++ b/tests/ExceptionTests.cpp @@ -4,12 +4,15 @@ #include "../src/Core/Exceptions/CreatureException.hpp" TEST_CASE("When throwing exception, what() is readable", "[Utilities]") { + bool hasCaught = false; try { throw CreatureException("foobar"); - } catch (const CreatureException& e) { + } catch (const std::exception& e) { + hasCaught = true; INFO(e.what()); REQUIRE(strcmp(e.what(), "foobar") == 0); } + REQUIRE(hasCaught); } #endif \ No newline at end of file