php - What is $previous in Exception class? -
what $previous
in exception
constructor argument denote? how use it?
class myexception extends \exception { private $message; private $code; public function __construct($message,$code,\exception $previous=null){ $this->message = $message; $this->code = isset($code) ? $code : 0; parent::__construct($message,$code,$previous); } }
i didn't find in api doc
if throw exception because caught exception, can add original exception $previous
. means can "nest" exceptions:
try { throw new fooexception('foo exception'); } catch (fooexception $e) { $code = 1; throw new barexception('bar exception', $code, $e); }
you can loop on exception "stack" instead of exception caught, providing more context.
while($e instanceof \exception) { echo $e->getmessage(); $e = $e->getprevious(); }
now, use if you're implementing library can throw exception, you'd wrap exception in own. way, clients' code needs know of your exceptions, , not of exceptions dependencies of code has, while not losing kind of information.
Comments
Post a Comment