|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2014-12-29 14:03 UTC] clement dot herreman at gmail dot com
 Description:
------------
When throwing an exception is the callback of array_map or array_filter, an E_WARNING is triggered ("Warning: array_filter(): An error occurred while invoking the filter callback in /your/script.php on line XX").
If no error handler is set, then the warning is directly sent to the output.
If an error handler is set via set_error_handler then: 
  * The error handler is not called at all
  * The warning simply disappear and are nowhere to be found
Test script:
---------------
<?php
/* Sample script: `php this.php` to see the warning in the output
 *                `php this.php 1` to try to setup an error handler
 */
if (!empty($argv[1])) {
    set_error_handler(function ($errno, $errstr) {
        printf("(%d) %s\n", $errno, $errstr);
    });
}
function failFilter()
{
    try {
        array_filter([1, 2, 3, 4], function ($n) {
            throw new \Exception('Some exception');
        });
    } catch (\Exception $e) {
    }
}
function failMapException()
{
    try {
        array_map(function ($n) {
            throw new \Exception('oops');
        }, [1, 2, 3]);
    } catch (\Exception $e) {
    }
}
function failMapTriggerError()
{
    try {
        array_map(function ($n) {
            trigger_error('Some error');
        }, [1, 2, 3]);
    } catch (\Exception $e) {
    }
}
failFilter();
failMapException();
failMapTriggerError();
Expected result:
----------------
(1024) Some exception
(1024) Some exception
(1024) Some exception
(1024) oops
(1024) oops
(1024) oops
(1024) Some error
(1024) Some error
(1024) Some error
Actual result:
--------------
cherreman@mlil-cherreman ~ $ php test.php
PHP Warning:  array_filter(): An error occurred while invoking the filter callback in /Users/cherreman/test.php on line 14
Warning: array_filter(): An error occurred while invoking the filter callback in /Users/cherreman/test.php on line 14
PHP Warning:  array_map(): An error occurred while invoking the map callback in /Users/cherreman/test.php on line 25
Warning: array_map(): An error occurred while invoking the map callback in /Users/cherreman/test.php on line 25
PHP Notice:  Some error in /Users/cherreman/test.php on line 35
Notice: Some error in /Users/cherreman/test.php on line 35
PHP Notice:  Some error in /Users/cherreman/test.php on line 35
Notice: Some error in /Users/cherreman/test.php on line 35
PHP Notice:  Some error in /Users/cherreman/test.php on line 35
Notice: Some error in /Users/cherreman/test.php on line 35
cherreman@mlil-cherreman ~ $ php test.php 1
(1024) Some error
(1024) Some error
(1024) Some error
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 09:00:01 2025 UTC | 
It seems that issue #55416 has not fixed this. The warning still occurs: PHP 5.6.4-4ubuntu6.3 (cli) (built: Sep 29 2015 12:44:47). array_reduce(['a'], function () { throw new \Exception(); });