Reputation: 4521
By default Xdebug will dump any exception regardless of whether it is caught or not:
try {
throw new Exception();
}
catch (Exception $e) {
}
echo 'life goes on';
With XDebug enabled and the default settings this piece of code will actually output something like the following (nicely formatted):
( ! ) Exception: in /test.php on line 3 Call Stack
# Time Memory Function Location 1 0.0003 52596 {main}( ) ../test.php:0
life goes on
Is it possible to disable this behaviour and have it dumping only the uncaught exceptions?
Thanks in advance.
UPDATE: I'm about to conclude that this is a bug, since xdebug.show_exception_trace is disabled by default yet it doesn't behave as expected (using Xdebug v2.0.5 with PHP 5.2.10 on Ubuntu 9.10).
Upvotes: 7
Views: 4945
Reputation: 20566
If your code is namespaced, the catch block should reference \Exception
-- with the backslash -- if there's no backslash then PHP will look for Exception
in your current namespace. This usually fails and the uncaught exception is passed to Xdebug.
The following code passes the exception to Xdebug:
namespace foo;
try {
new \PDO(0);
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Uncaught exception...
Adding a backslash before Exception will look for (and find) Exception
in the global namespace:
namespace foo;
try {
new \PDO(0);
} catch (\Exception $e) {
echo "Caught!";
}
// Exception caught correctly
Manually throwing exceptions can be confusing (which is why I used PDO above). If we try to throw an Exception from the current namespace, PHP tells us Exception doesn't exist there:
namespace foo;
try {
throw new Exception();
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Class 'foo\Exception' not found
Throwing a global exception without a global reference in the catch block fails differently:
namespace foo;
try {
throw new \Exception(); // global Exception
} catch (Exception $e) {
echo "Caught!";
}
// Fatal error: Uncaught exception 'Exception' in...
In light of all this, it's probably a good idea to always prefix the catch's Exception
with a backslash.
namespace foo;
try {
throw new \Exception();
} catch (\Exception $e) {
echo "Caught!";
}
// Exception caught correctly
Upvotes: 1
Reputation: 97835
Change the xdebug.show_exception_trace option (note it's not enabled by default).
xdebug.show_exception_trace
Type: integer, Default value: 0
When this setting is set to 1, Xdebug will show a stack trace whenever an exception is raised - even if this exception is actually caught.
Upvotes: 10