You are here

Advanced exception handling in Drupal 7

Submitted by jbrown on Tue, 09/21/2010 - 15:42

Earlier this year Drupal core developers were getting a strange error message while developing Drupal 7:

Fatal error: Exception thrown without a stack frame in Unknown on line 0

This was causing a lot of head scratching because it doesn't provide any information about what caused it.

Drupal 7 uses exceptions more than any previous version of Drupal and this turned out to be the problem.

PHP exceptions are handled in one of two ways:

  • A function further down the call stack catches the exception in a try{} catch{}.
  • The exception reaches the bottom of the call stack and the exception handler (_drupal_exception_handler() in Drupal 7) is invoked.

Unfortunately there are certain contexts of PHP execution where the exception handler will not be invoked - causing the above error to be output:

  1. exception handler (already)
  2. session save handler
  3. registered shutdown function
  4. class destructor method invoked during PHP shutdown

My patches to fix contexts 1 - 3 are in Drupal 7. The solution involved each of these contexts having their own try{} catch{} block so that the function had a no throw guarantee. In this situation, details about the exception are now output in a very simple manner and are not logged so nothing further can go wrong. For the case of already being in the exception handler, both the original exception and the addition one are output.

There isn't a general solution for context 4. Class destructors should not throw exceptions.