外部の try..catch ブロック内にいるかどうかを判断することはできますか?
コード例 (例として取り上げてください) :
<?php
class Foo{
public function load($id)
{
try{
// Model throw NodeNotFoundException only in rare cases
$node = $this->getModel()->loadById($id);
}
catch(NodeNotFoundException $nle)
{
// @here I need to tell if im in the First case or in the Second one,
// detecting the external try..catch block
if(externalTryCatchBlock() === true)
{
throw $nle;
}
else
{
watchdog('Unable to find node', $nle->details);
}
return false;
}
catch(Exception $e)
{
watchdog('Something gone wrong.');
return null;
}
return $node;
}
}
$foo = new Foo();
// First case, no external try..catch
$node = $foo->load(2);
// Second case: we need to do here something different if the node load
// throw an exception
try{
$another_node = $foo->load(3);
}
catch(NodeNotFoundException $nle)
{
watchdog('Unable to find node, using default.');
$another_node = Bar::defaultNode(); // This is JUST for example
}
// Do something with $another_node
?>
基本的に、例外 ( ) を再スローする必要NodeNotFoundException
があるのは、それを待っている別の catch ブロックがある場合のみですFatal error: Uncaught exception
。
もちろん、上記の例では、2 つのロード方法 (try..catch を使用する方法と使用しない方法) を使用できますが、これは避けたいと思います..そして、try を検出できるかどうか知りたいです。 PHP の .catch ブロック