PHP finally blocks not run on exit

I confirmed with the following code that if you call ‘exit’ withing a ‘try’ block the ‘finally’ block does *not* execute. That’s probably what you would expect. But now we know.

register_shutdown_function( 'handle_shutdown' );

try {

  exit;

}
catch ( Exception $ex ) {

  echo "caught...\n";

}
finally {

  echo "finally...\n";

}

function handle_shutdown() {

  echo "shutdown...\n";

}

Comments are closed.