13.12. Processing Exceptions
The only drawback to using method calls to detect particular types of exceptions: if ( X::TooBig->caught( ) ) { is that you have to be careful about the order in which you try your alternatives. For example, if X::WaaaaayTooBig inherits from X:TooBig, the following code won't work correctly: The problem is that if an X::WaaaaayTooBig exception is thrown, $EVAL_ERROR will refer to an X::WaaaaayTooBig object. But the X::WaaaaayTooBig class inherits from the X::TooBig class, so an X::WaaaaayTooBig object is an X::TooBig object. That means the first if test will succeed, and the specialized derived-class exception will be treated like a generic base-class exception instead. The solution is simple: whenever you're determining the type of an exception you just caught, test for the most-derived classes first. |