downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Exception::getTraceAsString> <Exception::getLine
[edit] Last updated: Fri, 10 Feb 2012

view this page in

Exception::getTrace

(PHP 5 >= 5.1.0)

Exception::getTraceRécupère la trace de la pile

Description

final public array Exception::getTrace ( void )

Retourne la trace de la pile de l'exception.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

Retourne la trace de la pile de l'exception sous la forme d'un tableau.

Exemples

Exemple #1 Exemple avec Exception::getTrace()

<?php
function test() {
 throw new 
Exception;
}

try {
 
test();
} catch(
Exception $e) {
 
var_dump($e->getTrace());
}
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

array(1) {
  [0]=>
  array(4) {
    ["file"]=>
    string(22) "/home/bjori/tmp/ex.php"
    ["line"]=>
    int(7)
    ["function"]=>
    string(4) "test"
    ["args"]=>
    array(0) {
    }
  }
}



add a note add a note User Contributed Notes Exception::getTrace
andreas at cap-systems dot com 09-Jun-2010 12:55
When calling getTrace(), there is also the name of the class in returned array:

<?php
 
class Test {

    function
__construct() {

      throw new
Exception('FATAL ERROR: bla bla...');

    }

  }

  try {

   
$obj = new Test();

  } catch(
Exception $e) {

   
var_dump($e->getTrace());

  }
?>

Will show something like:

array(1) {
  [0]=>  array(6) {
               ["file"]=>  string(54) "/....../test.php"
               ["line"]=>  int(37)
               ["function"]=>  string(11) "__construct"
               ["class"]=>  string(4) "Test"
               ["type"]=>  string(2) "->"
               ["args"]=>  array(0) { }
             }
}

You can use this function to format a exception:

<?php
 
function MakePrettyException(Exception $e) {
   
$trace = $e->getTrace();

   
$result = 'Exception: "';
   
$result .= $e->getMessage();
   
$result .= '" @ ';
    if(
$trace[0]['class'] != '') {
     
$result .= $trace[0]['class'];
     
$result .= '->';
    }
   
$result .= $trace[0]['function'];
   
$result .= '();<br />';

    return
$result;
  }

 
//Example:
 
try {

   
$obj = new Test();

  } catch(
Exception $e) {

    echo
MakePrettyException($e);

  }

?>

Result:

Exception: "FATAL ERROR: bla bla..." @ Test->__construct();

 
show source | credits | sitemap | contact | advertising | mirror sites