|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62985 set_exception_handler doesn't work from command line
Submitted: 2012-08-31 16:40 UTC Modified: 2012-09-04 14:46 UTC
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lgandras at gmail dot com Assigned:
Status: Wont fix Package: *Configuration Issues
PHP Version: 5.3.16 OS: CentoOS 6.2 x64
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-08-31 16:40 UTC] lgandras at gmail dot com
This is the output of my console:

# /usr/local/bin/php -v
PHP 5.3.16 (cli) (built: Aug 30 2012 18:38:54) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
# /usr/local/bin/php -r 'set_exception_handler(function(){echo "catched\n";});throw new Exception;'

Fatal error: Uncaught exception 'Exception' in Command line code on line 1

Exception:  in Command line code on line 1

Call Stack:
    0.0002     632056   1. {main}() Command line code:0


Test script:
# /usr/local/bin/php -r 'set_exception_handler(function($e){echo "catched!\n";});throw new Exception;'

Expected result:

Actual result:
Fatal error: Uncaught exception 'Exception' in Command line code on line 1

Exception:  in Command line code on line 1

Call Stack:
    0.0002     632056   1. {main}() Command line code:0


bug62985.patch (last revision 2012-08-31 17:29 UTC by

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-31 17:25 UTC]
The following patch has been added/updated:

Patch Name: bug62985.patch
Revision:   1346433947
 [2012-08-31 17:28 UTC]
a quick fix has been attached. but it is a change of zend API, so maybe someone 
else will have objections
 [2012-08-31 17:29 UTC]
The following patch has been added/updated:

Patch Name: bug62985.patch
Revision:   1346434197
 [2012-08-31 18:14 UTC] lgandras at gmail dot com
Temporary solution

echo '<?php set_exception_handler(function($e){echo "catched\n";});throw new Exception;' | /usr/local/bin/php
 [2012-09-04 14:46 UTC]
-Status: Open +Status: Wont fix
 [2012-09-04 14:46 UTC]
since you have got a alternative way,, then I'd like this be marked as wont'fix
 [2014-07-26 19:38 UTC] php at PointedEars dot de
I see the same in PHP 5.6.0RC2 on Debian GNU/Linux. This does not appear to be a configuration issue as the same script handles the exception when run with CLI as a file with the *same* configuration. The manual does not say anything about that, but someone reported the this problem in the user comments 8 years ago.  Why is this bug marked WONTFIX?

Steps to reproduce the problem (from bash command line):

$ cat php55-exceptions.php 

set_exception_handler(function (Exception $ex) {
  echo 42 . PHP_EOL;

throw new Exception();

$ sed -n '2,$p' php55-exceptions.php

set_exception_handler(function (Exception $ex) {
  echo 42 . PHP_EOL;

throw new Exception();

$ php -r "$(sed -n '2,$p' php55-exceptions.php)"
PHP Fatal error:  Uncaught exception 'Exception' in Command line code:6
Stack trace:
#0 {main}
  thrown in Command line code on line 6

$ php php55-exceptions.php 

Versions and configuration:

$ php -r 'phpinfo(INFO_GENERAL);'
PHP Version => 5.6.0RC2
System => Linux pe-mobil2.localdomain 3.4.87-pe #1 SMP Mon Apr 21 15:20:23 CEST 2014 i686
Build Date => Jul 22 2014 22:49:54
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
Scan this dir for additional .ini files => /etc/php5/cli/conf.d
Additional .ini files parsed => /etc/php5/cli/conf.d/05-opcache.ini,

PHP API => 20131106
PHP Extension => 20131226
Zend Extension => 220131226
Zend Extension Build => API220131226,NTS
PHP Extension Build => API20131226,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => disabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => enabled
Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, mcrypt.*, mdecrypt.*
This program makes use of the Zend Scripting Language Engine:
Zend Engine v2.6.0-dev, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.4, Copyright (c) 2002-2014, by Derick Rethans

$ dpkg -l 'php5*' | awk '/^.i/ {print $2, $3}'
php5 5.6.0~rc2+dfsg-5
php5-apcu 4.0.6-1
php5-cgi 5.6.0~rc2+dfsg-5
php5-cli 5.6.0~rc2+dfsg-5
php5-common 5.6.0~rc2+dfsg-5
php5-curl 5.6.0~rc2+dfsg-5
php5-gd 5.6.0~rc2+dfsg-5
php5-imagick 3.1.2-1+b1
php5-intl 5.6.0~rc2+dfsg-5
php5-json 1.3.5-3
php5-mcrypt 5.6.0~rc2+dfsg-5
php5-memcache 3.0.8-4+b1
php5-mysql 5.6.0~rc2+dfsg-5
php5-readline 5.6.0~rc2+dfsg-5
php5-xdebug 2.2.4-1+b1
php5-xsl 5.6.0~rc2+dfsg-5

Changes to php.ini:

$ diff -u /usr/share/php5/php.ini-production.cli /etc/php5/cli/php.ini
--- /usr/share/php5/php.ini-production.cli      2014-07-23 01:11:03.000000000 +0200
+++ /etc/php5/cli/php.ini       2014-06-02 13:05:31.000000000 +0200
@@ -83,8 +83,6 @@
 ; development version only in development environments, as errors shown to
 ; application users can inadvertently leak otherwise secure information.
-; This is php.ini-production INI file.
 ; Quick Reference ;
@@ -231,7 +229,7 @@
 ; Development Value: 4096
 ; Production Value: 4096
-output_buffering = 4096
+output_buffering = Off
 ; You can redirect all of the output of your scripts to a function.  For
 ; example, if you set output_handler to "mb_output_handler", character
@@ -276,7 +274,7 @@
 ; implications and is generally recommended for debugging purposes only.
 ; Note: This directive is hardcoded to On for the CLI SAPI
-max_execution_time = 30
+max_execution_time = 30000
 ; Maximum amount of time each script may spend parsing request data. It's a good
 ; idea to limit this time on productions servers in order to eliminate unexpectedly
@@ -710,6 +708,7 @@
 ; UNIX: "/path1:/path2"
 ;include_path = ".:/usr/share/php"
+include_path = ".:/usr/share/php:/home/pelinux/LCARS/scripts/php"
 ; Windows: "\path1;\path2"
 ;include_path = ".;c:\php\includes"
@@ -882,7 +881,7 @@
 ; Defines the default timezone used by the date functions
-;date.timezone =
+date.timezone = Europe/Zurich
 ;date.default_latitude = 31.7667
@@ -1686,7 +1685,6 @@
 ;mbstring.language = Japanese
-; Use of this INI entry is deprecated, use global internal_encoding instead.
 ; internal/script encoding.
 ; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*)
 ; If empty, default_charset or internal_encoding or iconv.internal_encoding is used.


I do not have a debug build of PHP.  Please tell me if I need to make one in this case (not a PHP crash).
 [2015-05-13 20:54 UTC] daniel dot beardsley at gmail dot com
Just ran into this same issue.

I'm suspicious that this isn't the only thing that just *doesn't* work when run from 'php -r'
 [2016-11-03 10:24 UTC] kastaneda at gmail dot com
The same incorrect behavior in interactive mode:

$ php -a
Interactive mode enabled

php > set_exception_handler(function(){echo "catched\n";});
php > throw new Exception;
PHP Warning:  Uncaught Exception in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1
php > ^D
$ php -v
PHP 7.0.8-0ubuntu0.16.04.3 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.8-0ubuntu0.16.04.3, Copyright (c) 1999-2016, by Zend Technologies
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Aug 21 03:01:27 2019 UTC