php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63873 Implement exception chaining within core classes
Submitted: 2012-12-29 16:26 UTC Modified: 2013-01-03 16:33 UTC
From: mattsch at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 5.3.20 OS: Gentoo
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mattsch at gmail dot com
New email:
PHP Version: OS:

 

 [2012-12-29 16:26 UTC] mattsch at gmail dot com
Description:
------------
php core classes have no way of continuing the exception chain.  If we are to have proper exception chaining as implemented in the exception class in php 5.3, we should also have the ability to continue the exception chain within php core classes to make debugging easier.

Test script:
---------------
<?php
class MyCustomException extends Exception {}

function doStuff() {
    try {
        throw new InvalidArgumentException("You are doing it wrong!", 112);
    } catch(Exception $e) {
        try {
                $pdo = new PDO('foo', 'foo', 'bar', array()); // exception chain lost
                // $pdo = new PDO('foo', 'foo', 'bar', array(), $e); // needs additional previous exception parameter
        } catch (Exception $ex) {
                throw new MyCustomException("Something happened", 911, $ex);
        }
    }
}


try {
    doStuff();
} catch(Exception $e) {
    do {
        printf("%s:%d %s (%d) [%s]\n", $e->getFile(), $e->getLine(), $e->getMessage(), $e->getCode(), get_class($e));
    } while($e = $e->getPrevious());
}


Expected result:
----------------
Expected result:
----------------
foo.php:13 Something happened (911) [MyCustomException]
foo.php:10 invalid data source name (0) [PDOException]
foo.php:7 You are doing it wrong! (112) [InvalidArgumentException]

Actual result:
--------------
Actual result:
--------------
foo.php:13 Something happened (911) [MyCustomException]
foo.php:10 invalid data source name (0) [PDOException]

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-03 16:33 UTC] mattsch at gmail dot com
-Status: Open +Status: Closed
 [2013-01-03 16:33 UTC] mattsch at gmail dot com
It turns out that this is not necessary.  The workaround for this is to catch core exceptions and throw a new exception and pass the previous exception (core exception) into the new exception.  Then the exception chain won't be broken.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Apr 19 20:01:24 2021 UTC