Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resume PHP to execution script after exception

Tags:

php

try-catch

I have a php script lets say during execution the scripts throws an exception. I want my PHP to resume from where it left off (where it had thrown the exception).

Should I put the same execution code in the "catch" part of the code?

On example, is lets say connects to mySQL it fails for connection timed out

   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();
   }
   try {
        someCode();
        }   
    } catch (PDOException $e) {  
        //re-execute same code as within the try clause?
        someCode();
    }
like image 246
MCHam Avatar asked Dec 19 '12 15:12

MCHam


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.

How do you use PHP to handle exceptions?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

Will finally be executed after return PHP?

finally statement, the finally block still executes after the return statement. The result will be returned after the finally block is executed. Also, if the finally block has a return statement, the value from the finally block will be returned.

Which statement continues execution of program if error generated?

The continue statement will restart the loop (as opposed to the break statement, which terminates the loop). As such if you replace break; with continue; , you will keep on looping after your Exception is caught (providing no other Exception is thrown but the one caught), ans the error message is displayed.


2 Answers

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 */
}
like image 52
Sherif Avatar answered Nov 16 '22 00:11

Sherif


Use php 4/5 register_shutdown_function.

Doc here: http://php.net/manual/en/function.register-shutdown-function.php

like image 33
gf4gs56dg4sd56s Avatar answered Nov 16 '22 01:11

gf4gs56dg4sd56s