[Insight-users] throwing exception inside constructor

Hauke Heibel heibel at cs.tum.edu
Wed Oct 17 16:59:53 EDT 2007


Hi Luis,

I think your post from before was absolutely ok. The link you provided

http://nedbatchelder.com/blog/20041202T070735.html

describes exactly the same issue that arose in Ziv's case and when you come
to their fix... well, they tried to prevent exceptions within the
constructor. The problem is that if the constructor of any LightObject
derived object throws, m_ReferenceCount will always be bigger than zero and
such the itkExceptionMacro (l. 190, itkLightObject.cxx) will be triggered.

So actually the ITK version of Ziv's example should (probably) look like the
sample given at the end of this e-mail. I think we are even safe in case
void *LightObject::operator new(size_t n) throws, since in that case, the
LightObject's constructor is not yet called and thus nothing should happen.

Just following the discussion from before I agree that usually exceptions
from constructors should be perfectly valid and of course also that
exceptions thrown from destructors are utterly evil....

Best regards,
Hauke

----- sample code -----

#include "itkCommand.h"

class MyClass : public itk::Command
{
public:
  typedef MyClass Self;
  typedef itk::SmartPointer<Self> Pointer;
  
  itkNewMacro(Self);
  
  virtual void Execute(itk::Object *caller,
    const itk::EventObject & event ) {}
  virtual void Execute(const itk::Object *caller,
    const itk::EventObject & event ){}
  
  void Print()
  {
    std::cout<<i<<"\n";
  }
  
  void Initialize(int i)
  {
    if (i<0)
    {
      throw std::runtime_error("got negative");
    }
    this->i =i;
  }
  
private:
  MyClass(){}
  ~MyClass(){}
  int i;
};

int main(int argc, char *argv[])
{
  int i = -1;

  try {
    MyClass::Pointer m = MyClass::New();
    m->Initialize(i);
    m->Print();
  }  //what I expect
  catch(std::runtime_error &re) {
    std::cout<<re.what()<<"\n";
  }  //maybe an itk exception
  catch(itk::ExceptionObject &eo) {
    std::cout<<eo.what()<<"\n";
  }  //last resort
  catch(...) {
    std::cout<<"caught exception\n";
  }
}

-----Original Message-----
From: insight-users-bounces+heibel=cs.tum.edu at itk.org
[mailto:insight-users-bounces+heibel=cs.tum.edu at itk.org] On Behalf Of Luis
Ibanez
Sent: Wednesday, October 17, 2007 9:50 PM
To: Brad King
Cc: insight-users at itk.org; 'Ziv Yaniv'
Subject: Re: [Insight-users] throwing exception inside constructor



Hi Ziv,

So, I was clearly wrong in my previous email.

It seems that your Exception should have worked fine
in the constructor.

Could you please post to the list a minimal source code
example that illustrate the crash that you are observing ?


We could then track the problem from there.


    Thanks


       Luis



------------------
Brad King wrote:
> Niels Dekker wrote:
> 
>>Luis Ibanez wrote:
>>
>>>Hi Ziv,
>>>
>>>Throwing exceptions from constructors is in general discouraged.
>>
>>It may be discouraged to throw exceptions from constructors of ITK based
>>objects (if you say so), but it certainly isn't discouraged "in general".
>>
>>Bjarne Stroustrup even recommends doing so!  From Bjarne's C++ Style and
>>Technique FAQ:  "You should throw an exception from a constructor
>>whenever you cannot properly initialize (construct) an object. There is
>>no really satisfactory alternative to exiting a constructor by a throw."
>>www.research.att.com/~bs/bs_faq2.html#ctor-exceptions
> 
> 
> This is correct.  Throwing from *destructors* is discouraged, and IMO
> should be outright banned by convention.  Throwing from constructors is
> the proper thing to do when the object cannot be initialized into a
> valid state.  Usually this means some resource could not be allocated.
> 
> The question here is whether ITK convention allows allocation of
> resources during construction (other than perhaps memory for sub-objects
> allocated dynamically).  Since there are no constructor arguments for
> objects in the LightObject hierarchy, I think the convention discourages
> allocation of resources by constructors.
> 
> -Brad
> 
_______________________________________________
Insight-users mailing list
Insight-users at itk.org
http://www.itk.org/mailman/listinfo/insight-users



More information about the Insight-users mailing list