PEAR 基底クラス (Previous) (Next) PEAR_Exception を使用した、PHP 5+ における高度なエラー処理

View this page in Last updated: Sun, 06 Jul 2008
English | French | German | Japanese | Polish | Plain HTML

PEAR_Exception - PHP 5 以降のバージョン

PEAR_Exception は、PHP 5 対応の PEAR パッケージで推奨されるエラー処理方法です。 PEAR_Exception は、PHP 5 に組み込まれている Exception クラスの軽量なラッパーです。エラーの原因を指定したり オブザーバを登録したり、その他多くの機能を持っています。

一般に、例外は例外的な場合にのみ使用するものです。 例えば、実行を停止しなければならないようなエラーです。 PEAR_Exception の主な使用目的はエラーの情報を既存のパッケージの外部に提供することで、 一般的なフロー制御は行いません。 制御構造 を使用して例外の処理をしてください。

PEAR 基底クラス (Previous) (Next) PEAR_Exception を使用した、PHP 5+ における高度なエラー処理

Download Documentation Last updated: Sun, 06 Jul 2008
Do you think that something on this page is wrong? Please file a bug report or add a note.
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.