ITK icon indicating copy to clipboard operation
ITK copied to clipboard

DataObjectError print to output stream should also print info about its DataObject

Open N-Dekker opened this issue 1 month ago • 1 comments

Description

When an error message appears, saying something like Requested region is (at least partially) outside the largest possible region, it would be very helpful to also print some information about those regions. Or more in general, print some information about the data object for which the error occurred.

Looking at the implementation of DataObjectError::PrintSelf, it appears that the intention is indeed to print such information, as it does m_DataObject->PrintSelf: https://github.com/InsightSoftwareConsortium/ITK/blob/a9e3442997bcbfb2dc6361dc700c291964b720cf/Modules/Core/Common/src/itkDataObject.cxx#L73-L87

However, DataObjectError::PrintSelf is never called! When someone prints a DataObjectError, for example by std::cerr << error, it will call operator<<(std::ostream &, const ExceptionObject &), which calls Print, not PrintSelf!!! Here

https://github.com/InsightSoftwareConsortium/ITK/blob/a9e3442997bcbfb2dc6361dc700c291964b720cf/Modules/Core/Common/include/itkExceptionObject.h#L152-L155

Steps to Reproduce

  const auto           image = itk::Image<int>::New();
  itk::DataObjectError error(__FILE__, __LINE__);
  error.SetDescription("My description");
  error.SetDataObject(image);
  std::cerr << error << std::endl;

Expected output

itk::DataObjectError (0000004659AFEE28)
File: path/to/source/file
Line: 42
Description: My description

Data object:
  RTTI typeinfo:   class itk::Image<int,2>
  Reference Count: 1
  Modified Time: 1
  Debug: Off
  Object Name:
  Observers:
    none
  Source: (none)
  Source output name: (none)
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 0
  UpdateMTime: 0
  RealTimeStamp: 0 seconds
  LargestPossibleRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [0, 0]
  BufferedRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [0, 0]
  RequestedRegion:
    Dimension: 2
    Index: [0, 0]
    Size: [0, 0]
  etc. etc....

Actual output

itk::DataObjectError (0000004659AFEE28)
File: path/to/source/file
Line: 42
Description: My description

So no information at all about the data object!

Reproducibility

100%

Versions

ITK 5 (v5.4.5), ITK 6 (v6.0b01), current main (commit f51594ad88194a72cdc06a314dafff709fcdc2e9)

Environment

Any environment

Additional Information

Reported to me by my colleague Denis (@dpshamonin). Encountered while using SimpleITK.

For the record

Both DataObjectError and ExceptionObject got a PrintSelf member function with commit 78e889ed46d36131f5de552d76bd5326121222ae, Sept 13, 2001. However, even then, operator<<(std::ostream& os, ExceptionObject &e) still just called Print, not PrintSelf. Eventually ExceptionObject lost its PrintSelf with commit 19ae435967f74302693c825a193ba8d99f6eb357, Feb 26, 2002.

N-Dekker avatar Nov 28 '25 17:11 N-Dekker

Thank you for contributing an issue! 🙏

Welcome to the ITK community! 🤗👋☀️

We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜 Also, please check existing open issues and consider discussion on the ITK Discourse. 📖

This is an automatic message. Allow for time for the ITK community to be able to read the issue and comment on it.

github-actions[bot] avatar Nov 28 '25 17:11 github-actions[bot]