Exception::getPrevious

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Exception::getPreviousDevuelve la Throwable anterior

Descripción

final public function Exception::getPrevious(): ?Throwable

Devuelve la Throwable anterior (el tercer parámetro de Exception::__construct()).

Parámetros

Esta función no contiene ningún parámetro.

Valores devueltos

Devuelve la Throwable anterior si está disponible o null si no.

Ejemplos

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());
}
?>

Resultado del ejemplo anterior es similar a:

/home/bjori/ex.php:8 Ocurrió algo (911) [MiPropiaExcepción]
/home/bjori/ex.php:6 ¡Lo está haciendo mal! (112) [InvalidArgumentException]

Ver también