Skip to content
Advertisement

Does a PHP exception stop execution?

<?php
     function some_function($arg) {
         if($filter->check_for_safe_input($arg)) {
             throw new Exception("Hacking Attempt");
         }
         do_some_database_stuff($arg);
     }
 ?>

In the above code example, does do_some_database_stuff ever get called if check_for_safe_input fails, or does the exception stop the function running? It’s something I’ve never quite been sure of, and usually I just stick functions like do_some_database_stuff in an else statement to be sure, but this tends to lead to hugely nested functions.

Advertisement

Answer

Yes, uncaught exceptions result in fatal errors that stop the execution of the script. So the do_some_database_stuff function will not be called if an exception is thrown. You can read more about exceptions in this article.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement