Exception::getPrevious

(PHP 5 >= 5.3.0, PHP 7)

Exception::getPreviousZwraca poprzedni wyjątek

Opis

final public Exception::getPrevious ( void ) : Throwable

Zwraca poprzedni wyjątek (trzeci parametr Exception::__construct()).

Parametry

Ta funkcja nie posiada parametrów.

Zwracane wartości

Zwraca poprzedni Throwable lub NULL jeśli nie jest dostępny.

Rejestr zmian

Wersja Opis
7.0.0 Deklaracja zwracanego typu została zmieniona na Throwable.

Przykłady

Przykład #1 Przykład użycia Exception::getPrevious()

Wyświetlanie śladu wyjątków za pomocą pętli.

<?php
class MyCustomException extends Exception {}

function 
doStuff() {
    try {
        throw new 
InvalidArgumentException("Robisz to źle!"112);
    } catch(
Exception $e) {
        throw new 
MyCustomException("Coś się stało"911$e);
    }
}


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

Powyższy przykład wyświetli coś podobnego do:

/home/bjori/ex.php:8 Coś się stało (911) [MyCustomException]
/home/bjori/ex.php:6 Robisz to źle! (112) [InvalidArgumentException]

Zobacz też:

add a note add a note

User Contributed Notes 2 notes

up
5
harry at upmind dot com
5 years ago
/**
     * Gets sequential array of all previously-chained errors
     *
     * @param Throwable $error
     *
     * @return Throwable[]
     */
    function getChain(Throwable $error) : array
    {
        $chain = [];

        do {
            $chain[] = $error;
        } while ($error = $error->getPrevious());

        return $chain;
    }
up
-6
Anonymous
12 years ago
May be I am late (5.2 is not supported). But if you want use the functionality of "previous Exception" in PHP < 5.3 or write compatible code, you can use next way as below.

<?php
abstract class App_Exception_PreviousNativeAbstract extends Exception {
    public static
$printPrevious = true;

    public function
__toString() {
       
$result   = array();
       
$result[] = sprintf("Exception '%s' with message '(%s) %s' in %s:%d", get_class($this), $this->code, $this->message, $this->file, $this->line);
       
$result[] = '---[Stack trace]:';
       
$result[] = $this->getTraceAsString();

        if (
self::$printPrevious) {
           
$previous = $this->getPrevious();
            if (
$previous) {
                do {
                   
$result[] = '---[Previous exception]:';
                   
$result[] = sprintf("Exception '%s' with message '(%s) %s' in %s:%d", get_class($previous), $previous->getCode(), $previous->getMessage(), $previous->getFile(), $previous->getLine());
                   
$result[] = '---[Stack trace]:';
                   
$result[] = $previous->getTraceAsString();
                } while(
method_exists($previous, 'getPrevious') && ($previous = $previous->getPrevious()));
            }
        }

        return
implode("\r\n", $result);
    }
}

abstract class
App_Exception_PreviousLegacyAbstract extends App_Exception_PreviousNativeAbstract {
    protected
$previous;

    public function
__construct($message, $code = 0, Exception $previous = null) {
       
$this->previous = $previous;

       
parent::__construct($message, $code);
    }

    public function
getPrevious() {
        return
$this->previous;
    }
}

if (
version_compare(PHP_VERSION, '5.3.0', '>=')) {
    abstract class
App_Exception_PreviousAbstract extends App_Exception_PreviousNativeAbstract {}
}
else {
    abstract class
App_Exception_PreviousAbstract extends App_Exception_PreviousLegacyAbstract {}
}

class
App_Exception extends App_Exception_PreviousAbstract {
    public function
__construct($message, $code = 0, Exception $previous = null) {
       
parent::__construct($message, 0, $previous);
    }
}

// Example:
try {
   
// ...
   
throw new Exception('Exception mesage');
   
// ...
} catch (Exception $e) {
    throw new
App_Exception('App exception mesage', 0, $e);
}
?>
To Top