Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a PHP exception stop execution?

Tags:

exception

php

<?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.

like image 229
Not Joe Bloggs Avatar asked Feb 20 '10 12:02

Not Joe Bloggs


People also ask

Does throwing an exception stop execution PHP?

When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block. If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.

Does exception stop execution?

except block is completed and the program will proceed. However, if an exception is raised in the try clause, Python will stop executing any more code in that clause, and pass the exception to the except clause to see if this particular error is handled there.

Does exception catch all exceptions PHP?

Catching all PHP exceptions Because exceptions are objects, they all extend a built-in Exception class (see Throwing Exceptions in PHP), which means that catching every PHP exception thrown is as simple as type-hinting the global exception object, which is indicated by adding a backslash in front: try { // ... }

What is a PHP exception?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.


2 Answers

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.

like image 100
Darin Dimitrov Avatar answered Sep 27 '22 22:09

Darin Dimitrov


Have a look at the PHP manual on exceptions.

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().

php.net/exceptions

So yes, the rest of the function is not being executed, a fatal error occurs instead.
If you catch the exception, execution of the script continues in the corresponding catch block, everything "between" the function which throws an exception and the catch block is not executed.

like image 24
svens Avatar answered Sep 28 '22 00:09

svens