Oh, the ever-dreaded “Exception thrown without a stack frame” error. It will haunt you in your sleep. It will steal the spacebar from your keyboard, cut your mouse cord, and uninstall your operating system. It is the bane of all programming existence!
This is possibly the most esoteric error that can be thrown by PHP, but once you know it, you’ll never forget it.
The problem arises when using a custom exception handler, which is typical of many frameworks (you probably landed here because your framework just spit this out at you, right?).
I’m not sure what the heck it means, but you will get this error when throwing an Exception within a custom Exception handler that you have previously set. I presume this is due to some kind of infinite looping scenario, which I shall refer to as the infinite looping scenario problem.
Code Example
// Custom Exception handler.
function myExceptionHandler($exception)
{
// Exception within an Exception. It's Exception Inception. Ahhhhhhh!
throw new Exception('Should result in weird stack frame error!');
}
// Set our custom Exception handler.
set_exception_handler('myExceptionHandler');
throw new Exception('Error!');
When running this code, you should get:
Fatal error: Exception thrown without a stack frame in Unknown on line 0
The Solution
Don’t make mistakes in your custom Exception handler! Seriously. This is the method you’ve declared will solve all problems. You must ensure that you don’t throw any Exceptions in this function. Keep the logic simple. Avoid complex logging that can result in additional Exceptions when the database is down, wrap try/catch blocks around possible culprits, do whatever it takes. Even if you can’t ensure that you won’t throw Exceptions in your custom handler, at least you know what the error means now, which may help you debug the problem more quickly in the future!