Php: Catch Exception and Continue Execution, Is It Possible

php: catch exception and continue execution, is it possible?

Sure, just catch the exception where you want to continue execution...

try
{
SomeOperation();
}
catch (SomeException $ignored)
{
// do nothing... php will ignore and continue
// but maybe use "ignored" as name to silence IDE warnings.
}

Of course this has the problem of silently dropping what could be a very important error. SomeOperation() may fail causing other subtle, difficult to figure out problems, but you would never know if you silently drop the exception.

Continue code execution after an error and deal with them later in PHP?

try/catch will definitely work for this. Since you're specifically concerned with continuing execution after the exception, keep in mind that it matters what is included in the try block. For example:

$array = [1, 2, 3, 'string', 5, 6];

try {
foreach ($array as $number) {
if (is_string($number)) throw new Exception("Not a number", 1);
echo $number;
}
} catch (Exception $e) {
echo $e->getMessage();
}

with this code you would see

123Not a number

because after the exception is handled, execution will continue after the catch block, not at the point the exception was thrown.

Whereas, with the try/catch inside the foreach loop

foreach ($array as $number) {
try {
if (is_string($number)) throw new Exception("Not a number", 1);
echo $number;
} catch (Exception $e) {
echo $e->getMessage();
}
}

the loop would continue after the exception and you would see

123Not a number56

Handle exception and continue executing

This is just bad idea. I just hope you don't understand how Exceptions work and you're not meaning the question.

First of all, setting exception handler... Exception handler is called when the exceptions is propagated to main script (actually out of it) and your script is therefore done:

Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

You should either use what's Xeoncross suggesting, but I think you have problem with called function/method that is throwing exceptions so you can do this:

class Clazz {

private static $exceptions;

public static function foo(array $exceptions) {
set_exception_handler(array(__CLASS__, "exception_handler"));
self::$exceptions = $exceptions;
try {
throw new RandomException;
} catch( Exception $e){
self::exception_handler( $exception);
}
echo "I need this to be printed!";
}

public static function exception_handler(Exception $exception) {
}
}

Resume PHP to execution script after exception

First of all one should make clear that an exception is only fatal if it is not caught. Catching an exception does not halt script execution. It merely stops the stack frame in the try block and transfers control to the catch block. From there your script will continue to execute as normal.

By catching the exception here we still resume normal script execution after the exception is caught...

try {
echo "Try...\n";
throw new Exception("This is an exception");
} catch(Exception $e) {
echo "Exception caught with message: " . $e->getMessage() . "\n";
}

echo "Script is still running...";

There's another way to handle uncaught exceptions, using an exception handler. However if you don't use a try and catch statement, execution flow will still be halted. This is the nature of exceptions:

function myExceptionHandler($e) {
echo "Uncaught exception with message: " , $e->getMessage(), "\n";
}

set_exception_handler('myExceptionHandler'); // Registers the exception handler

throw new Exception("This is Exception 1");
echo "Execution never gets past this point";
throw new Exception("This is Exception 2");
throw new Exception("This is Exception 3");

Edit: After clarifying your question I think that I should state what you want is not an exception handler, but you actually don't want to use Exceptions at all. What you're trying to do does not require throwing Exceptions at all. Don't put PDO into exception mode if what you intend to do is just handle the error like that. Exception should only be used to handle exceptional errors. The whole point of an exception is to make sure you keep your promise. For example, if your function makes the promise that it will always return a PDOStatement object and there is a case where it can not possibly do that, then it makes sense to throw an Exception. This lets the caller know that we have reached a point where we can not keep our promise.

What you want is basic error handling...

function someCode(){
$pdostmt = $this->prepare($this->sql);
if($pdostmt->execute($this->bind) !== false) {
if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))
return $pdostmt->fetchAll($this->fetchOption);
elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql))
return $pdostmt->rowCount();
} else {
return false;
}
}

while (someCode() === false) {
/* Call someCode() until you get what you want */
}

in a php loop, how to continue to the next iteration if the current one craches

If your using php7: php7 throws errors like exceptions. And all recoverable errors are catchable. And also both errors and exceptions implement a common interface called Throwable.

That means you can surround your call with a try-catch-block and simply continue the loop, when a throwable error occurs:

foreach ($array as $row) {
try {
$row->executeThatFunction();
} catch (Throwable $t) {
// you may want to add some logging here...
continue;
}
}

Using continue in try-catch to prevent script termination when exception is thrown

There is no need to add a continue keyword.

If an exception is thrown in the try block, the code in the catch block will be executed. After that the rest of the code will be executed normally.

Read the section about exceptions in the language reference for details.

Does a PHP exception stop execution?

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.



Related Topics



Leave a reply



Submit