Harts Harts - 2 years ago 93
PHP Question

PHP Uncaught exception gives more details than caught exception

I build some api endpoint, and trying to work on logging exception, so I purposely remove database and run the endpoint to get PDOException.

My question is, if I did not caught the exception, when I ran the endpoint through postman, it shows me a much more detailed message, stack trace, etc (about 1000 lines),

but if I caught the exception with try catch block something like this

catch (\Exception $exception) {


It is so much less details. Why is that? and is there anyway to print the same exact data that we got when we don't caught the exception?

Answer Source

You have all that data. Exception object has many methods:

try {
    // something throwing exception
} catch (\Exception $e) {
    echo $e->getCode() . "\n";
    echo $e->getFile() . "\n";
    echo $e->getLine() . "\n";
    echo $e->getMessage() . "\n";
    echo $e->getTraceAsString() . "\n";

Read more about Exception class:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download