1. Using PEAR_Exception
    for advanced error
    handling in PHP 5+

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:

  • Nestable exceptions (

    <?php throw new PEAR_Exception($msg$prev_exception); ?>

    )

  • Subject/Observer pattern, triggered when an exception is instantiated

  • Clear, detailed and attractively formatted error messages

  • Extra context information available compared to built-in Exception. For instance, a cause of the exception (PEAR_Error/PEAR_ErrorStack/another Exception).

  • Exception cause can be a PEAR_Error object, PEAR_Exception object or an array of mixed PEAR_Exceptions/PEAR_ErrorStack warnings

  • callbacks for specific exception classes and their children

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.

PEAR_Exception - PHP 5 and versions above (Previous) PEAR_ErrorStack (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

User Notes:

There are no user contributed notes for this page.