Comments By: Mo
Given that it's unlikely that you'd create instances of exceptions that you aren't going to throw, why not put the rollback code in __construct() instead? That way, if the exception is dealt with by a handler registered via set_exception_handler() which doesn't use __toString() (for example, it just prints a simple error page and bailsâ€”not that I'd recommend that, but the developer might not have got as far as implementing the exception logging part of the handler yet), the rollback code is still executed. I'll grant, putting it in the constructor is less elegant, but it seems more likely that __toString() would never be called than an exception class instance being constructed in a scenario other than it about to be thrownâ€”and given that you control the code throwing AND the exception's implementation itself, but DON'T control the code handling the exception, putting it in the constructor seems the safest choice.
posted on Dec 30, 2007 at 5:40am |