为什么我的异常没有被捕获? [英] Why isn't my Exception being caught by catch?
问题描述
我有一些看起来像这样的代码
I have some code that looks like this
# Try to import file
try
{
DataManager::fileImport($_FILES['datafile']['tmp_name'],
$_POST['zones'], $_POST['statuses']);
}
catch(Exception $e)
{
print 'Herp.';
$response->body = Helpers::getVarDump($e);
}
DataManager :: fileImport
从字面上看是引发正常异常的单行函数:
DataManager::fileImport
is literally a one-line function that throws a normal Exception:
static function fileImport($filepath, $zones, $statuses)
{
throw new Exception('SOME EXCEPTION');
}
但是我得到了
致命错误:未捕获的异常'Exception',消息为'SOME EXCEPTION'...
Fatal error: Uncaught exception 'Exception' with message 'SOME EXCEPTION'...
从try块。同样, Herp。也不会打印。为什么异常不会触发catch块?
From the try block. Also 'Herp.' is never printed. Why doesn't the Exception trigger the catch block?
编辑:我应该提到我正在使用Tonic 和PHP 5.3.9
I should mention I'm using Tonic and PHP 5.3.9
再次编辑:这是DataManager(名称替换为。 ..表示匿名) http://pastebin.com/daHWBJDC
EDIT AGAIN: Here's DataManager (with names replaced with ... for anonymity) http://pastebin.com/daHWBJDC
推荐答案
解决方案
我忽略了在文件中指定使用\Exception;
我知道有意让PHP中的每个名称空间都应该定义自己的名称出于多种原因导致异常,但是我仍然感到奇怪的是,当 Exception
catch(Exception e)不会引起任何错误>在那种情况下没有定义。如果我要编写 new Exception()
我会得到一个错误。
I know it's intentional that each namespace in PHP should define its own Exception for many reasons, but I still find it odd that catch(Exception e)
didn't cause any errors when Exception
in that context wasn't defined. If I were to write new Exception()
I would get an error.
哦,至少我知道了东西。
Oh well, at least I learned something.
这篇关于为什么我的异常没有被捕获?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!