1

I suppose that the exception system of PHP will catch all. but it doesn't.

try{ 
    $obj = new Asdfasdfasdf()
} catch(Exception $e){
    trace(...something...)
}

But it doesn't catch this kind of error, and I have searched php document, which didn't say which kind of exception/error is catch-able in try,catch clause.

So, how can I know that which kind of exception/error will be catched by my catch clause ?

P.S.

I finnally understand the 'error' from php engine is not the 'exception' from use land code. If you want use exception to handle engine 'error', you should manually wrap all 'error' in exception.

9
  • 2
    A fatal error is not an exception, exceptions are exceptions.... I suggest you start by reading about exceptions Commented Dec 7, 2015 at 16:37
  • 1
    fatal errors cannot be caught. they don't throw anything. if you want to "catch" this sort of thing, you have to register an error handler/shutdown script. Commented Dec 7, 2015 at 16:37
  • @MarkBaker have read the doc, please tell me, in which line, the doc says that exception cannot catch error, and which kind of error cannot be catched by exception Commented Dec 7, 2015 at 16:55
  • @MarkBaker I come from c++, and all error in c++ can be handle in exception Commented Dec 7, 2015 at 16:56
  • 1
    And the relevant link to ErrorException with an example of how to convert errors to exceptions Commented Dec 7, 2015 at 17:00

1 Answer 1

1

If you want to throw an Exception in the event that a class does not exist it, you could use class_exists().

A naive example might look something like:

function createClass($class)
{
    if (!class_exists($class)) {
        throw new Exception(
            sprintf('Class %s does not exist', $class)
        );
    }

    return new $class;
}

try {
    $asdfasdfasdf = createClass('Asdfasdfasdf');
} catch (Exception $e) {
    echo $e->getMessage();
}

From my experience, most PHP frameworks throw some sort of exception when a class is not found - for example, Symfony2 throws a ClassNotFoundException. That said, I don't know if you can 'catch' that, it's probably really just a development aid.

PHP 7 has just been released and from what I understand from the spec, you will be able to catch a fatal error as an EngineException. I don't know if it would work for your example; I haven't tested it because I have not installed PHP 7 stable yet. I tried your example with an alpha release of PHP 7 on an online REPL, and it appears that it does not work.

However for completeness, here's an example from the RFC:

function call_method($obj) {
    $obj->method();
}

try {
    call_method(null); // oops!
} catch (EngineException $e) {
    echo "Exception: {$e->getMessage()}\n";
}

// Exception: Call to a member function method() on a non-object

In any case, as noted by @MarkBaker and @MarcB in the question's comments, you cannot "catch" a fatal error in previous versions of PHP.

Hope this helps :)

Sign up to request clarification or add additional context in comments.

2 Comments

thank u! it really help me understand that the error and exception are two different system in PHP, if you want catch "error", you have to manually wrap "error" in a exception
No problem @lovespring :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.