How to implement exception chaining in PHP

Posted by Josef Sábl on Stack Overflow See other posts from Stack Overflow or by Josef Sábl
Published on 2010-04-09T08:46:36Z Indexed on 2010/04/09 8:53 UTC
Read the original article Hit count: 359

Filed under:
|
|

Constructor for PHP's exception has third parameter, documentation says:

$previous: The previous exception used for the exception chaining. 

But I can't make it work. My code looks like this:

try
{
    throw new Exception('Exception 1', 1001);
}
catch (Exception $ex)
{
    throw new Exception('Exception 2', 1002, $ex);
}

I expect Exception 2 to be thrown and I expect that it will have Exception 1 attached. But all I get is:

Fatal error: Wrong parameters for Exception([string $exception [, long $code ]]) in ...

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about php

Related posts about exception