Using PEAR_Exception for advanced error handling in PHP 5+

Using PEAR_Exception for advanced error handling in PHP 5+  --  Using PEAR_Exception

Synopsis

Introduction to the usage of PEAR_Exception

Introduction

This class is available as part of the PEAR package. Features include:

Usage example:
<?php
require_once 'PEAR/Exception.php';

class Test {
	function foo() {
		throw new PEAR_Exception('Error Message', ERROR_CODE);
    }
}

function myLogger($pear_exception) {
    echo $pear_exception->getMessage();
}
// each time a exception is thrown the 'myLogger' will be called
// (its use is completely optional)
PEAR_Exception::addObserver('myLogger');
$test = new Test;
try {
    $test->foo();
} catch (PEAR_Exception $e) {
    print $e;
}
?>

API documentation is documented in the documentation for the PEAR package generated by phpDocumentor. The class is very simple, examine the source in the PEAR package to get a better idea of how it works.