From 0a363faafb5c951499b47fc9fc1b930b78501eac Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Fri, 28 Nov 2025 18:14:13 +0100 Subject: [PATCH] BUG: Let DataObjectError override `ExceptionObject::Print` Instead of introducing a new virtual member function, `DataObjectError::PrintSelf`. It is essential that DataObjectError overrides `ExceptionObject::Print`, because that is the member function that is called, when doing `std::cerr << error` for a DataObjectError. - Fixes issue https://github.com/InsightSoftwareConsortium/ITK/issues/5666 "DataObjectError print to output stream should also print info about its DataObject" --- Modules/Core/Common/include/itkDataObject.h | 12 ++---------- Modules/Core/Common/src/itkDataObject.cxx | 10 +++------- 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/Modules/Core/Common/include/itkDataObject.h b/Modules/Core/Common/include/itkDataObject.h index 26811fd85aa..b08d3a2cd42 100644 --- a/Modules/Core/Common/include/itkDataObject.h +++ b/Modules/Core/Common/include/itkDataObject.h @@ -90,8 +90,8 @@ class ITKCommon_EXPORT DataObjectError : public ExceptionObject * specific exception subtypes. The default is to print out the * location where the exception was first thrown and any description * provided by the "thrower". */ - virtual void - PrintSelf(std::ostream & os, Indent indent) const; + void + Print(std::ostream & os) const override; private: DataObject * m_DataObject{ nullptr }; @@ -126,14 +126,6 @@ class ITKCommon_EXPORT InvalidRequestedRegionError : public DataObjectError /** \see LightObject::GetNameOfClass() */ itkOverrideGetNameOfClassMacro(InvalidRequestedRegionError); - -protected: - /** Print exception information. This method can be overridden by - * specific exception subtypes. The default is to print out the - * location where the exception was first thrown and any description - * provided by the "thrower". */ - void - PrintSelf(std::ostream & os, Indent indent) const override; }; /*----------------------------Data Object--------------------------------*/ diff --git a/Modules/Core/Common/src/itkDataObject.cxx b/Modules/Core/Common/src/itkDataObject.cxx index edab7cb616f..5a9c70fc694 100644 --- a/Modules/Core/Common/src/itkDataObject.cxx +++ b/Modules/Core/Common/src/itkDataObject.cxx @@ -70,10 +70,12 @@ DataObjectError::GetDataObject() const noexcept void -DataObjectError::PrintSelf(std::ostream & os, Indent indent) const +DataObjectError::Print(std::ostream & os) const { ExceptionObject::Print(os); + const Indent indent{}; + os << indent << "Data object: "; if (m_DataObject) { @@ -99,12 +101,6 @@ InvalidRequestedRegionError::InvalidRequestedRegionError(const InvalidRequestedR InvalidRequestedRegionError & InvalidRequestedRegionError::operator=(const InvalidRequestedRegionError &) noexcept = default; -void -InvalidRequestedRegionError::PrintSelf(std::ostream & os, Indent indent) const -{ - DataObjectError::PrintSelf(os, indent); -} - //---------------------------------------------------------------------------- DataObject::DataObject() : m_SourceOutputName("")