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 */
}
tryblock can contain many statements. It's possible to find out which one triggered the exception but it isn't trivial. What are you trying to accomplish?