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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
43 - 21 = ?
Subscribe to this entry?

 
 [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: Sun May 05 18:01:31 2024 UTC