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();
}
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…
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:
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…