PHP5 Exception handling
prev

Good Design

Exceptions should always be used as an addon and not as the base for Error Handling. This means, you won't get around coding carefully and checking for error conditions wherever you can.

I usually call this whole thing 'pessimistic programming' (starts with customer-quality-standards vs developers-quality-standards, goes via agile methods and ends in pessimistic view of code and possible events.
 function move($type)
 {
	// no (valid) parameter? exit
	if(!$type) return false;

	// important pre-condition fails
 	if(!$this->isValidMove(&$type))
		return false;

	// important step fails
 	if(!$this->startAction())
		throw new FailedActionException(&$type);

	// ok
	return true;
 }
 
Using this approach, you'll save some nested blocks (like deep nested 'if') and have shorter, snappiert code, handling errors where they occur. You'll get a feeling on when to use Exceptions, and when to use standard handling.

Questions?

Feel free to contact andreas@php.net if you have further questions concerning exceptions.
prev