2015-06-09 62 views
0

我想我不會使用Imagine libray正確管理異常。如何在CakePHP 3中處理Imagine異常

我的代碼是:

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(); 
    } 

} 

但試想例外,我不明白它和我的腳本將停止。

我的錯誤在哪裏?

回答

1

您正在使用合格的名稱,導致它相對於當前名稱空間解析,即Imagine\Exception\Exception將解析爲\CurrentNamespace\Imagine\Exception\Exception,並且由於該名稱不存在,因此您沒有捕獲任何內容。

既可以使用導入的命名空間,這是Exception,即Exception\Exception,這將解析爲\Imagine\Exception\Exception,或使用適當的全名,也就是一個名字開始與\,即\Imagine\Exception\Exception

又見PHP Manual > Language Reference > Namespaces > Using namespaces: Basics

+0

好吧,我明白了。謝謝。 – 2ndGAB