Skip to content

Commit ea54c7e

Browse files
authored
Merge pull request #5266 from N-Dekker/Add-location-to-ExceptionObject-what
ENH: Add location (function) to text returned by ExceptionObject::what()
2 parents ef37881 + ebcf175 commit ea54c7e

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

Modules/Core/Common/src/itkExceptionObject.cxx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ class ExceptionObject::ExceptionData
4242
, m_Line(line)
4343
{
4444
m_What = m_File;
45-
m_What += ':' + std::to_string(m_Line) + ":\n";
45+
m_What += ':' + std::to_string(m_Line) + ':';
46+
if (!m_Location.empty())
47+
{
48+
m_What += " in '" + m_Location + "':";
49+
}
50+
m_What += '\n';
4651
m_What += m_Description;
4752
}
4853

Modules/Core/Common/test/itkExceptionObjectGTest.cxx

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,48 @@ TEST(ExceptionObject, TestDescriptionFromSpecializedExceptionMacro)
8484

8585

8686
// Tests that `ExceptionObject::what()` returns the expected concatenation of
87-
// file name, line number, and description.
87+
// file name, line number, location and description.
8888
TEST(ExceptionObject, TestWhat)
8989
{
90-
const itk::ExceptionObject exceptionObject(__FILE__, __LINE__, "test description");
90+
{
91+
// Test with empty location.
92+
const itk::ExceptionObject exceptionObject(__FILE__, __LINE__, "test description", "");
9193

92-
const char * const what = exceptionObject.what();
93-
const char * const file = exceptionObject.GetFile();
94-
const char * const description = exceptionObject.GetDescription();
94+
const char * const what = exceptionObject.what();
95+
const char * const file = exceptionObject.GetFile();
96+
const char * const description = exceptionObject.GetDescription();
9597

96-
ASSERT_NE(what, nullptr);
97-
ASSERT_NE(file, nullptr);
98-
ASSERT_NE(description, nullptr);
98+
ASSERT_NE(what, nullptr);
99+
ASSERT_NE(file, nullptr);
100+
ASSERT_NE(description, nullptr);
99101

100-
EXPECT_EQ(what, file + (":" + std::to_string(exceptionObject.GetLine()) + ":\n") + description);
102+
EXPECT_EQ(what, file + (":" + std::to_string(exceptionObject.GetLine()) + ":\n") + description);
103+
}
104+
{
105+
// Test with location = ITK_LOCATION (as used by ITK's exception macro's).
106+
const itk::ExceptionObject exceptionObject(__FILE__, __LINE__, "test description", ITK_LOCATION);
107+
108+
const char * const what = exceptionObject.what();
109+
const char * const file = exceptionObject.GetFile();
110+
const char * const description = exceptionObject.GetDescription();
111+
const char * const location = exceptionObject.GetLocation();
112+
113+
ASSERT_NE(what, nullptr);
114+
ASSERT_NE(file, nullptr);
115+
ASSERT_NE(description, nullptr);
116+
ASSERT_NE(location, nullptr);
117+
118+
119+
EXPECT_EQ(what,
120+
file + (":" + std::to_string(exceptionObject.GetLine()) + ": in '" + location + "':\n") + description);
121+
122+
try
123+
{
124+
itkGenericExceptionMacro("Oops, something went exceptionally wrong!");
125+
}
126+
catch (const std::exception & exception)
127+
{
128+
std::cout << exception.what() << '\n';
129+
}
130+
}
101131
}

0 commit comments

Comments
 (0)