php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77520 Fatal error while catching re-thrown exception
Submitted: 2019-01-25 14:56 UTC Modified: 2019-01-25 15:13 UTC
From: mimipim at abv dot bg Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.2.14 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mimipim at abv dot bg
New email:
PHP Version: OS:

 

 [2019-01-25 14:56 UTC] mimipim at abv dot bg
Description:
------------
https://3v4l.org/iA7rS

Test script:
---------------
<?php

class TestException extends Exception{}

try {
    throw new TestException();
} catch(TestException $e) {
    var_dump('catched TestException');
    throw $e;
} catch (Exception $e) {
    var_dump('catched exception');
}

Expected result:
----------------
I am expecting to catch both exceptions but only the first is catched and than a fatal error occurs.

Actual result:
--------------
string(21) "catched TestException" Fatal error: Uncaught TestException in /in/iA7rS:6 Stack trace: #0 {main} thrown in /in/iA7rS on line 6
Process exited with code 255.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-25 15:13 UTC] danack@php.net
-Status: Open +Status: Not a bug
 [2019-01-25 15:13 UTC] danack@php.net
I think you've misunderstood how try/catch blocks work.

A catch block only catches exceptions from within the try block it is attached to. It does not catch exceptions from the other catch blocks.

This is deliberate to allow 'recasting' some exceptions to other exception types.


try {
 ...
}
catch (SpecificException $se) {
    throw new OtherException("this is a wrapped exception", $se);
}
catch (Exception $e) {
   throw $e;
}
 [2019-01-25 15:14 UTC] danack@php.net
btw you may wish to set an uncaught exception handler, to avoid seeing the "Fatal error: Uncaught TestException" message.

http://php.net/manual/en/function.set-exception-handler.php
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC