为什么我从闭包中抛出的异常没有被捕获?

时间:2022-11-29 20:18:38

I've written a PHPUnit test that checks if an exception is thrown from a closure when a method is invoked. The closure function is passed in as an argument to the method with an exception being thrown from it.

我编写了一个PHPUnit测试,它检查在调用方法时是否从闭包抛出异常。闭包函数作为参数传递给方法,并抛出异常。

public function testExceptionThrownFromClosure()
{
    try {
        $this->_externalResourceTemplate->get(
            $this->_expectedUrl,
            $this->_paramsOne,
            function ($anything) {
                throw new Some_Exception('message');
            }
        );

        $this->fail("Expected exception has not been found");
    } catch (Some_Exception $e) {
        var_dump($e->getMessage()); die;
    }
}

The code for the get function specified on the ExternalResourceTemplate is

在ExternalResourceTemplate上指定的get函数的代码是

public function get($url, $params, $closure)
{
    try {
        $this->_getHttpClient()->setUri($url);
        foreach ($params as $key => $value) {
            $this->_getHttpClient()->setParameterGet($key, $value);
        }
        $response = $this->_getHttpClient()->request();
        return $closure($response->getBody());
    } catch (Exception $e) {
        //Log
        //Monitor
    }
}

Any ideas why the fail assert statement is called? Can you not catch exceptions thrown from closures in PHP or is there a specific way of dealing with them I don't know about.

有什么想法为什么要调用fail assert语句吗?你能不能捕捉到PHP闭包中抛出的异常,或者有没有一种我不知道的处理方法。

For me the exception should just propagate out the return stack, but it doesn't appear to. Is this a bug? FYI I'm running PHP 5.3.3

对我来说,异常应该只是传播返回堆栈,但它似乎并没有。这是一个错误吗?我正在运行PHP 5.3.3

2 个解决方案

#1


2  

Thanks for the answers...

谢谢你的回答…

Managed to figure out the issue. It looks like the problem is that the try-catch block that's being invoked is the one where the closure is invoked. Which makes sense...

设法解决了问题。看起来问题在于正在调用的try-catch块是调用闭包的地方。这是有道理的……

So the code above should be

上面的代码应该是

public function get($url, $params, $closure)
{
    try {
        $this->_getHttpClient()->setUri($url);
        foreach ($params as $key => $value) {
            $this->_getHttpClient()->setParameterGet($key, $value);
        }
        $response = $this->_getHttpClient()->request();
        return $closure($response->getBody());
    } catch (Exception $e) {
        //Log
        //Monitor
        throw new Some_Specific_Exception("Exception is actually caught here");
    }
}

So it looks like PHP 5.3.3 doesn't have a bug after all which was mentioned. My mistake.

看起来PHP 5.3.3没有出现bug。我的错误。

#2


0  

I cannot reproduce the behavior, my example script

我不能复制行为,我的示例脚本

<?php
class Some_Exception extends Exception { }
echo 'php ', phpversion(), "\n";
$foo = new Foo;
$foo->testExceptionThrownFromClosure();

class Foo {
    public function __construct() {
        $this->_externalResourceTemplate = new Bar();
        $this->_expectedUrl = '_expectedUrl';
        $this->_paramsOne = '_paramsOne';
    }

    public function testExceptionThrownFromClosure()
    {
        try {
            $this->_externalResourceTemplate->get(
                $this->_expectedUrl,
                $this->_paramsOne,
                function ($anything) {
                    throw new Some_Exception('message');
                }
            );

            $this->fail("Expected exception has not been found");
        } catch (Some_Exception $e) {
            var_dump('my exception handler', $e->getMessage()); die;
        }
    }
} 

class Bar {
    public function get($url, $p, $fn) {
        $fn(1);
    }
}

prints

打印

php 5.4.7
string(20) "my exception handler"
string(7) "message"

as expected

正如预期的

#1


2  

Thanks for the answers...

谢谢你的回答…

Managed to figure out the issue. It looks like the problem is that the try-catch block that's being invoked is the one where the closure is invoked. Which makes sense...

设法解决了问题。看起来问题在于正在调用的try-catch块是调用闭包的地方。这是有道理的……

So the code above should be

上面的代码应该是

public function get($url, $params, $closure)
{
    try {
        $this->_getHttpClient()->setUri($url);
        foreach ($params as $key => $value) {
            $this->_getHttpClient()->setParameterGet($key, $value);
        }
        $response = $this->_getHttpClient()->request();
        return $closure($response->getBody());
    } catch (Exception $e) {
        //Log
        //Monitor
        throw new Some_Specific_Exception("Exception is actually caught here");
    }
}

So it looks like PHP 5.3.3 doesn't have a bug after all which was mentioned. My mistake.

看起来PHP 5.3.3没有出现bug。我的错误。

#2


0  

I cannot reproduce the behavior, my example script

我不能复制行为,我的示例脚本

<?php
class Some_Exception extends Exception { }
echo 'php ', phpversion(), "\n";
$foo = new Foo;
$foo->testExceptionThrownFromClosure();

class Foo {
    public function __construct() {
        $this->_externalResourceTemplate = new Bar();
        $this->_expectedUrl = '_expectedUrl';
        $this->_paramsOne = '_paramsOne';
    }

    public function testExceptionThrownFromClosure()
    {
        try {
            $this->_externalResourceTemplate->get(
                $this->_expectedUrl,
                $this->_paramsOne,
                function ($anything) {
                    throw new Some_Exception('message');
                }
            );

            $this->fail("Expected exception has not been found");
        } catch (Some_Exception $e) {
            var_dump('my exception handler', $e->getMessage()); die;
        }
    }
} 

class Bar {
    public function get($url, $p, $fn) {
        $fn(1);
    }
}

prints

打印

php 5.4.7
string(20) "my exception handler"
string(7) "message"

as expected

正如预期的