¿Por qué no se detecta mi excepción por casualidad?

Tengo un código que se parece a esto

# 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 Tónico and PHP 5.3.9

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

preguntado el 01 de febrero de 12 a las 14:02

It should work. Are you perhaps not showing actual code, and throwing some RandomLyNamedException que no se extiende Exception? -

This looks correct. Are you sure that this is the only place where you are calling this fileImport method? If there are others, I would make sure they are also being caught. -

If using 5.3+, did you check your namespaces? -

I just made the class and that's the only place the function is being called. I also checked the line number in the stack trace -

This is the DataManager class pastebin.com/daHWBJDC (With some names replaced with ... for anonymity) -

4 Respuestas

solución

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

ponderando

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.

Respondido 15 Abr '15, 11:04

That is odd. Thanks for figuring this out. I come from a Java background and was completely lost as to why the try catch block wasn't interception an Exception. - James P.

Lol ... I just realized I'd left an exception handler in my data class. This explains that :p - James P.

Phew! Thanks for answering your own question. Not finding that out would have been a pain. - crantok

Sólo use Exception; will work. Imports by default import from the global namespace. - Jason

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.';
}


?>

Respondido 01 Feb 12, 18:02

That's pretty much identical to what I'm running. The only notable differences is that my class is in a separate file and namespace - Hubro

You might have an issue with your DataManager class because i copied your code, adapted it to run and i get the exception handled... You problem is elsewhere...

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

try{
    DataManager::fileImport('', '', '');
}catch(Exception $e){
    print 'Herp.';
}

Resultados en

Herp.

Respondido 01 Feb 12, 18:02

4 años después ...

@Hubro, thank you for saving me with that namespace fix!

It does seem counterintuitive at first that it's necessary when throwing a root-level Exception, even though it ultimately makes sense in the general context of namespaces.

For anyone who doesn't want to utilize @Hubro's file-level fix:

use \Exception;

You could instead add the backslash in front of Exception in the higher level catch block:

} catch (\Exception $e) {

We could all benefit from someone smarter than me providing suggestions on best practices around defining a custom Exception for each namespace. Any takers?

Respondido 30 Abr '18, 23:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.