Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
535 views
in Technique[技术] by (71.8m points)

php - 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 is literally a one-line function that throws a normal Exception:

static function fileImport($filepath, $zones, $statuses)
{
    throw new Exception('SOME EXCEPTION');
}

And yet I get

Fatal error: Uncaught exception 'Exception' with message 'SOME EXCEPTION'...

From the try block. Also 'Herp.' is never printed. Why doesn't the Exception trigger the catch block?


EDIT: I should mention I'm using Tonic and PHP 5.3.9

EDIT AGAIN: Here's DataManager (with names replaced with ... for anonymity) http://pastebin.com/daHWBJDC

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution

I neglected to specify use Exception; in the file containing the try/catch.

Pondering

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...