Sun, 05 Jun 2005

Exceptions revisited: the destructor relationship

So David White (of Wesnoth fame) made an excellent point on my previous problem with exceptions. The problem of exceptions unexpectedly exiting out the side doesn't exist if every object cleans up in destructors (ie. use RAII . In other words, all state changes must be encapsulated in a class.

This might seem obvious to people who deal with exceptions day-to-day (my C++ experience predates widespread use of exceptions). But encapsulating things in an object has one advantage: the programmer knows they cannot allocate memory in the destructor, The problem comes when not everything has a destructor, as will tend to happen when C code (and C programmers) using "exceptions" like my example.

[/tech] permanent link