20GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAStdException)
22 std::stringstream deathRegex;
24 deathRegex <<
"YourProgramName Fatal Error:\n An exception was thrown from " << std::exception().what();
32 throw std::exception();
34 catch (
const std::exception& except)
37 DryChem::ErrorMessage error1;
38 error1.programName =
"YourProgramName";
39 error1.message =
"An exception was thrown from " + std::string {except.what()};
41 throw DryChem::FatalException(error1);
44 catch (
const DryChem::FatalException& except)
46 except.handleErrorWithMessage();
52GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAFatalException)
54 std::stringstream deathRegex;
56 deathRegex <<
"YourProgramName Fatal Error:\n An exception was thrown from ";
58#if GTEST_USES_POSIX_RE
59 deathRegex <<
"[(]testExceptionHandling.hpp: *[0-9]*[)]";
60#elif GTEST_USES_SIMPLE_RE
61 deathRegex <<
"\\(testExceptionHandling.hpp: \\d*\\)";
64 deathRegex <<
"\n\tLocation message.\n";
72 throw DryChem::FatalException(
73 DryChem::ErrorMessage {
"Common-Utilities",
"Location message.", __FILE__, __LINE__});
75 catch (
const std::exception& except)
78 DryChem::ErrorMessage error1;
79 error1.programName =
"YourProgramName";
80 error1.message =
"An exception was thrown from " + std::string {except.what()};
82 throw DryChem::FatalException(error1);
85 catch (
const DryChem::FatalException& except)
87 except.handleErrorWithMessage();
93GTEST_TEST(testExceptionHandling, fatalErrorsAreHandledByTerminating)
95 std::stringstream deathRegex;
97 deathRegex <<
"Common-Utilities Fatal Error: ";
99#if GTEST_USES_POSIX_RE
100 deathRegex <<
"[(]testExceptionHandling.hpp: *[0-9]*[)]";
101#elif GTEST_USES_SIMPLE_RE
102 deathRegex <<
"\\(testExceptionHandling.hpp: \\d*\\)";
105 deathRegex <<
"\n\tThis would be the error message.\n";
109 DryChem::ErrorMessage error;
110 error.programName =
"Common-Utilities";
111 error.message =
"This would be the error message.";
112 error.fileName = __FILE__;
113 error.lineNumber = __LINE__;
115 DryChem::FatalException exceptFatal {error};
116 exceptFatal.handleErrorWithMessage();
121GTEST_TEST(testExceptionHandling, derivedExceptionClassIsCaughtByParentClass)
123 testing::internal::CaptureStderr();
127 DryChem::ErrorMessage error {};
128 error.programName =
"Common-Utilities";
129 error.message =
"Let's throw a non-fatal warning.";
130 error.fileName = __FILE__;
131 error.lineNumber = __LINE__;
133 throw DryChem::FatalException(error);
135 catch (
const std::exception& except)
137 std::cerr << except.what() << std::endl;
140 std::string actualOutput = testing::internal::GetCapturedStderr();
141 ASSERT_EQ(actualOutput,
"(testExceptionHandling.hpp: 131)\n\tLet's throw a non-fatal warning.\n");
GTEST_TEST(testExceptionHandling, thisIsHowWeWouldCatchAndHandleAStdException)
Definition testExceptionHandling.hpp:20