Skip to content

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

@N-Dekker

Description

@N-Dekker

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:

DataObjectError::PrintSelf(std::ostream & os, Indent indent) const
{
ExceptionObject::Print(os);
os << indent << "Data object: ";
if (m_DataObject)
{
os << std::endl;
m_DataObject->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << "(None)" << std::endl;
}
}

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

operator<<(std::ostream & os, const ExceptionObject & e)
{
e.Print(os);
return os;

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 f51594a)

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 78e889e, 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 19ae435, Feb 26, 2002.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions