Disabling Xdebug Throw Exception Exceptions

By default, Xdebug will throw any exception, regardless of whether it is caught or not:

try { throw new Exception(); } catch (Exception $e) { } echo 'life goes on'; 

With XDebug turned on and the default settings, this piece of code really outputs 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 behavior and throw only uncaught exceptions?

Thanks in advance.

UPDATE I am going to conclude that this is an error because xdebug.show_exception_trace is disabled by default, but it does not work properly (using Xdebug v2.0.5 with PHP 5.2.10 on Ubuntu 9.10).

+7
debugging php exception xdebug
source share
2 answers

Change the xdebug.show_exception_trace parameter (note that it is not enabled by default).

xdebug.show_exception_trace

Type: integer, Default value: 0

When this parameter is set to 1, Xdebug will display a stack trace whenever an exception occurs - even if that exception is actually caught.

+9
source share

If your code has a namespace, the catch block must reference \Exception - the backslash - if there is no backslash, then PHP will look for Exception in your current namespace. This is usually aborted, and the thrown exception is thrown by 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 an 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 says that Exception does not 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 happens 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 catch the catch Exception with a backslash.

 namespace foo; try { throw new \Exception(); } catch (\Exception $e) { echo "Caught!"; } // Exception caught correctly 
+1
source share

All Articles