Die Verwendung von PEAR_Exception zur Fehlerbehandling
wird empfohlen für Packages unter PHP 5. PEAR_Exception ist ein leichtgewichtiger
Wrapper für die eingebaute Exception-Klasse in PEAR,
ergänzt um die Möglichkeit Fehlerursachen anzugeben, Observer-Klassen
zu registrieren und weiteren Funktionen.
Exceptions sollten nur für ungewöhnliche Umstände verwendet werden - für Fehlerzustände,
welche die weitere Ausführung des Code unmögich machen. PEAR_Exception
sollte dazu verwendet werden, um Fehler nach aussen zu melden. Keinesfalls
sind sind ein Ersatz für die klassische Ablaufsteuerung in einen Programm;
die unter
Control Structures aufgeführten Möglichkeiten sollte der Vorzug geben werden.
|
PEAR - Die Basis-Klassen (Previous)
|
(Next)
Using PEAR_Exception for advanced error handling in PHP 5+
|
|
|
Download Documentation
|
Last updated: Sun, 29 Jun 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.
|
|