PEAR_Exception - PHP 5 and versions above

Table of Contents

PEAR_Exception is the recommended error handling solution for PHP 5-based packages in PEAR. PEAR_Exception is a lightweight wrapper above the built-in Exception class in PHP 5 that provides the ability to specify causes for errors, register observers, and many more features.

Exceptions in general should be used only for exceptional circumstances - for error conditions that require termination of execution. PEAR_Exception should mainly be used for transmitting error information outside the existing package, and not for normal flow control. Use Control Structures in favor of exceptions wherever possible.

OS independent PHP extension load (Previous) Using PEAR_Exception (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:

Note by: michele.manzato@bigfoot.com
There is a simple workaround for full PHP5 applications to convert "creating a PEAR_Error" to "throwing PEAR_Exception":

<?php
function PEAR_ErrorToPEAR_Exception($err)
{
    if (
$err->getCode()) {
        throw new 
PEAR_Exception($err->getMessage(), $err->getCode());
    }
    throw new 
PEAR_Exception($err->getMessage());
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK'PEAR_ErrorToPEAR_Exception');
?>


This way, each time a PEAR_Error is created in an old
PEAR package it is immediately converted and thrown as
a PEAR_Exception.

This is particularly useful for several old PEAR packages, and notably important ones such as MDB2, which still use PEAR_Error reporting and will continue to do so in order to maintain PHP4 compatibility.