Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't my Exception being caught by catch?

Tags:

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

like image 682
Hubro Avatar asked Feb 01 '12 14:02

Hubro


People also ask

What happens if exception is not caught in try catch?

If an exception is not caught (with a catch block), the runtime system will abort the program (i.e. crash) and an exception message will print to the console. The message typically includes: name of exception type.

Which exception Cannot be caught in catch block?

The only one that cannot be caught is StackOverflowException , and TreadAbortException gets rethrown at the end of the catch.

Does catch exception catch everything?

If you catch some Exception types and don't do anything with the information, you have no chance of knowing what went wrong in those situations, but if you catch all Exception subclasses you have no chance of knowing what went wrong in a much large number of situations.

Can exception be resolved in catch block?

The purpose of a try-catch block is to catch and handle an exception generated by working code. Some exceptions can be handled in a catch block and the problem solved without the exception being rethrown; however, more often the only thing that you can do is make sure that the appropriate exception is thrown.


Video Answer


2 Answers

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.

like image 141
Hubro Avatar answered Oct 05 '22 13:10

Hubro


Strange. If i run this code i get the "Herp."

<?php

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

# Try to import file
try
{
    DataManagerTest::fileImport("param1","param2","param3");
}
catch(Exception $e)
{
    print 'Herp.';
}


?>
like image 39
Andreas Helgegren Avatar answered Oct 05 '22 11:10

Andreas Helgegren