文字

Exception::getTrace

(PHP 5 >= 5.1.0)

Exception::getTrace获取异常追踪信息

说明

final public array Exception::getTrace ( void )

返回异常追踪信息。

参数

此函数没有参数。

返回值

返回包含异常追踪信息的数组( array )。

范例

Example #1 Exception::getTrace() 示例

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

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

以上例程的输出类似于:

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

用户评论:

[#1] knivey [2013-04-18 14:53:19]

The order of the trace starts at the source of the exception and does not include main.
So for example:

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

function 
Foo() {
 
Bar();
}

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


Will output:

array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(21) "/.../test.php"
    ["line"]=>
    int(8)
    ["function"]=>
    string(3) "Bar"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(21) "/.../test.php"
    ["line"]=>
    int(12)
    ["function"]=>
    string(3) "Foo"
    ["args"]=>
    array(0) {
    }
  }
}

[#2] sam at notmyrealemail dot org [2012-02-17 15:26:48]

Two important points about this function which are not documented:

1) The trace does not include the file / line at which the exception is thrown; that entry is only recorded in the top-level getFile/Line methods.

2) Elements are returned in 'closest-first' order, e.g. if you have a script x which calls function y which calls function z which throws an exception, then the first trace element will be 'Y' and the second will be 'X'.

[#3] andreas at cap-systems dot com [2010-06-09 12:55:28]

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

上一篇: 下一篇: