例外を再スローする場合、前の例外を 3 番目の引数として指定できます。こうすることで、例外トレースを連鎖させることができます。
try {
f('123');
} catch(Throwable $e){
var_dump($e);
}
function f($arg){
if(is_string($arg)){
try {
g($arg);
} catch(UnexpectedValueException $e) {
// Supply a third argument to pass the previous Exception.
throw new RuntimeException('Error in function g()', $e->getCode(), $e);
} catch(Throwable $e) {
// Supply a third argument to pass the previous Exception.
throw new RuntimeException('Unkown Error in function g()', $e->getCode(), $e);
}
}
}
function g($string){
if(strlen($string) < 6){
try {
h($string);
} catch(UnexpectedValueException $e) {
throw new UnexpectedValueException('String is smaller then 6', $e->getCode(), $e);
}
}
return $string;
}
function h($string){
if(strlen($string) < 4){
throw new UnexpectedValueException('String is smaller then 4');
}
return $string;
}
出力:
C:\wamp64\www\julian\index.php:21:
object(RuntimeException)[3]
protected 'message' => string 'Error in function g()' (length=21)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
protected 'line' => int 30
private 'trace' (Exception) =>
array (size=1)
0 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 19
'function' => string 'f' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
private 'previous' (Exception) =>
object(UnexpectedValueException)[2]
protected 'message' => string 'String is smaller then 6' (length=24)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
protected 'line' => int 43
private 'trace' (Exception) =>
array (size=2)
0 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 27
'function' => string 'g' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
1 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 19
'function' => string 'f' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
private 'previous' (Exception) =>
object(UnexpectedValueException)[1]
protected 'message' => string 'String is smaller then 4' (length=24)
private 'string' (Exception) => string '' (length=0)
protected 'code' => int 0
protected 'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
protected 'line' => int 51
private 'trace' (Exception) =>
array (size=3)
0 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 41
'function' => string 'h' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
1 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 27
'function' => string 'g' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
2 =>
array (size=4)
'file' => string 'C:\wamp64\www\julian\index.php' (length=30)
'line' => int 19
'function' => string 'f' (length=1)
'args' =>
array (size=1)
0 => string '123' (length=3)
private 'previous' (Exception) => null
public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> UnexpectedValueException: String is smaller then 4 in C:\wamp64\www\julian\index.php on line <i>51</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f( )</td><td title='C'... (length=1645)
public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> UnexpectedValueException: String is smaller then 6 in C:\wamp64\www\julian\index.php on line <i>43</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f( )</td><td title='C'... (length=1376)
public 'xdebug_message' => string '<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> RuntimeException: Error in function g() in C:\wamp64\www\julian\index.php on line <i>30</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361152</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\julian\index.php' bgcolor='#eeeeec'>...\index.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0034</td><td bgcolor='#eeeeec' align='right'>361528</td><td bgcolor='#eeeeec'>f( )</td><td title='C:\wamp64\ww'... (length=1096)