如何处理 CakePHP 3 中的 Imagine 异常

How to handle Imagine exception in CakePHP 3

我想我没有使用 Imagine libray 正确管理异常。

我的代码是:

use ....
use Imagine\Exception;
....

try {

    $imagine = new Imagine();

    $image = $imagine->open($img_path . DS . "tmpfile." . $extension)
        ->resize(new Box($cwidth, $cheight))
        ->crop(new Point($offsetx, $offsety), new Box(500, 500));

    ...

} catch (Imagine\Exception\Exception $e) {

    die("catch Imagine\Exception\Exception");
    $file = new File($img_path . DS . "tmpfile." . $extension);
    if ($file->exists()) {
        $file->delete();
    }

}

但是在 Imagine Exception 上,我没有捕捉到它并且我的脚本停止了。

我的错误在哪里?

您使用的是限定名称,导致它根据当前命名空间进行解析,即 Imagine\Exception\Exception 将解析为 \CurrentNamespace\Imagine\Exception\Exception,由于它不存在,您不抓住任何东西。

要么使用导入的命名空间,即 Exception,即 Exception\Exception,它将解析为 \Imagine\Exception\Exception,要么使用适当的完全限定名称,即名称以\,即 \Imagine\Exception\Exception.

另见 PHP Manual > Language Reference > Namespaces > Using namespaces: Basics