异常后将PHP恢复到执行脚本 [英] Resume PHP to execution script after exception

查看:164
本文介绍了异常后将PHP恢复到执行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,可以说在执行期间脚本会引发异常.我希望我的PHP从停止的地方(引发异常的地方)恢复.

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?

在示例中,可以说连接到mySQL失败,因为连接超时

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

推荐答案

首先应该明确指出,异常只有在未被捕获的情况下才是致命的.捕获异常不会停止脚本执行.它只是在try块中停止堆栈帧,然后将控制权转移到catch块.从那里开始,您的脚本将继续照常执行.

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...";

还有另一种方法来处理未捕获的异常,方法是使用异常处理程序.但是,如果您不使用try and catch语句,执​​行流程仍将停止.这是例外的性质:

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");

在澄清了您的问题之后,我认为我应该声明您想要的不是异常处理程序,但您实际上根本不想使用Exceptions.您要尝试执行的操作根本不需要抛出异常.如果您打算做的只是处理此类错误,请不要将PDO置于异常模式.异常只能用于处理例外错误.例外的全部目的是确保您信守诺言.例如,如果您的函数承诺将始终返回PDOStatement对象,并且在某些情况下无法做到这一点,则抛出Exception是有意义的.这使呼叫者知道我们已经达到了不能信守诺言的地步.

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 */
}

这篇关于异常后将PHP恢复到执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆