[vtk-developers] Exceptions

Brad King brad.king at kitware.com
Mon Mar 22 11:27:59 EST 2004


Ken Martin wrote:

> OK, Since this issue has been raised before let's take a couple minutes to
> discuss it. I have heard good and bad things about exceptions but I am not
> personally that familiar with them. So I'd like for some folks who have
> experience using exceptions in a large project to provide some input. From
> what I've seen here are the good and the bad:
> 
> (Good) Using exceptions allows for problems to be caught and handled in a
> deep stack such as pipeline execution without having to put in a lot of
> extra checks. I'm not 100% sure I buy this argument. Since a filter's input
> can be a pipeline or just a plain data object I don't see how the required
> testing is reduced. For many cases where a pipeline could produce a bad
> data/metadata the same bad metadata could be passed to the filter directly
> (without a pipeline). But I will agree that it could help.
> 
> (Good) Using exceptions allows for effectively testing many functions in one
> try block. This I think is a great feature. If anything ran out of memory in
> this try block then do this... is a great feature.
> 
> (Bad) There is a tendency to believe that throwing an exception makes
> everything OK. Exception based error handling seems to frequently lack
> appropriate try / catches. For example: some image writers in VTK will write
> out a volume as a series of slices. If the writer should run out of disk
> space then the error handling will actually go back and delete the slices
> already written so that an incomplete volume is not left on the disk. My
> concern is that with exceptions most people would just throw the exception
> and feel their job was done. I don't think this is a technical problem with
> exceptions but rather a bad coding practice where not enough effort is put
> into the try catch blocks that are required to make exceptions useful. (the
> same problem exists with not checking the return value of malloc etc, so it
> isn't limited to exceptions, but I think exceptions exacerbate the problem)

Properly working with exceptions will require a change to the VTK coding 
methodology.  For example, code like this is common in VTK:

vtkFoo* foo = vtkFoo::New();
// ... do something with foo ...
foo->Delete();

If an exception is thrown while working with foo, the object will leak. 
  All instances of code like this would have to be replaced with 
something like:

vtkAutoPointer<vtkFoo> foo = vtkFoo::New();
// ... do something with foo ...

or

vtkSmartPointer<vtkFoo> foo = vtkSmartPointer<vtkFoo>::New();
// ... do something with foo ...

That whether an exception is thrown or not, the reference to foo in this 
scope will be deleted.

Also, there may be some bad interaction between exceptions and 
DeleteEvent observers.  What if foo were destroyed during stack 
unwinding for an exception and a DeleteEvent handler were called?  Then 
an entire set of operations (which might also throw) would be executed 
while an uncaught exception is being handled.  A nested exception throw 
usually results in program termination.  This problem can be solved 
because it is possible for the event invoking code to detect if it is 
being executed with an uncaught exception, but we'll have to be sure to 
deal with this case.

-Brad




More information about the vtk-developers mailing list