By admin | August 2, 2009
If you attempt to throw an exception from a PHP destructor or an PHP exception handler, your script will execute fine for the most part and then crash with the rather cryptic error message, “Fatal error: Exception thrown without a stack frame in Unknown on line 0“. This message is unfortunately very cryptic if you are trying to debug your code. This effect is further amplified due to a lack of PHP debuggers and you must instead resort to using various print_r()s and var_dump()s in order to debug the program. The error message in basic form means that you’ve thrown an exception where an exception cannot be thrown (leading to “no stack frame” - exception handlers and destructors do not have a stack frame. This instead results in this rather cryptic error message that does not include script locations or detailed error information).
Most often, the error will appear if you use an exception handler combined with an error reporting to exception handler by converting it to an ErrorException, then there a suddenly a whole new magnitude of ways to throw errors within the exception handle, especially if E_NOTICE, E_STRICT and/or E_WARNING errors are converted. This form most often occurs when you use variables without first initializing them. This error may be preventable by wrapping the exception handler within a try/catch block.
A second form of this error occurs when you attempt to throw an exception in a destructor. This is well documented in the PHP manual, but this can still be triggered if you accidentally throw an exception:
- Manually - calling “throw new Exception(‘hello world’)’ in a destructor
- Implicitly - calling a function that throws an exception (e.g. calling function foo() which throws an exception)
- Error handler (ErrorException) - instating a user-defined function as an error handler which throws an ErrorException (which is still an error)
There is no exactly solid way to identify this type of error as you may not know exactly what your functions do. If you are throwing exceptions manually in your destructor, you need to go back to the PHP manually and read that it is not possible to throw exceptions within the destructor (as it lacks a stack frame). For the other two, you may need to comment out your destructors and manually uncomment parts until you reach the point where you find the error line as the error message produced by PHP is not too informative.
If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!