array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'es', ), 'this' => array ( 0 => 'exception.getprevious.php', 1 => 'Exception::getPrevious', ), 'up' => array ( 0 => 'class.exception.php', 1 => 'Exception', ), 'prev' => array ( 0 => 'exception.getmessage.php', 1 => 'Exception::getMessage', ), 'next' => array ( 0 => 'exception.getcode.php', 1 => 'Exception::getCode', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'es', 'path' => 'language/predefined/exception/getprevious.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
Exception::getPrevious — Devuelve la Throwable anterior
Devuelve la Throwable anterior (el tercer parámetro de Exception::__construct()).
Esta función no tiene parámetros.
Ejemplo #1 Ejemplo de Exception::getPrevious()
Recorrer, e imprimir la traza de una excepción.
<?php
class MiPropiaExcepción extends Exception {}
function hacerCosas() {
try {
throw new InvalidArgumentException("¡Lo está haciendo mal!", 112);
} catch(Exception $e) {
throw new MiPropiaExcepción("Ocurrió algo", 911, $e);
}
}
try {
hacerCosas();
} catch(Exception $e) {
do {
printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e));
} while($e = $e->getPrevious());
}
?>
El resultado del ejemplo sería algo similar a:
/home/bjori/ex.php:8 Ocurrió algo (911) [MiPropiaExcepción] /home/bjori/ex.php:6 ¡Lo está haciendo mal! (112) [InvalidArgumentException]