Php 如何使用异常而不使脚本消亡?

Php 如何使用异常而不使脚本消亡?,php,Php,我希望在代码的一部分中使用异常进行错误处理,但如果代码失败,我希望脚本继续。不过我想记录错误。有人能帮我弄清楚吗 试试看{ 如果($id==4) { 回声“测试”; } } 捕获(例外$e){ echo$e->getMessage(); } echo‘你好,你应该来看我……’ 在调用可能抛出do的代码的代码中 输出将为(为可读性添加换行符): Try/Catch块也可以嵌套。请参见上面链接的PHP手册页面中的示例2: try{ try { throw new Excep

我希望在代码的一部分中使用异常进行错误处理,但如果代码失败,我希望脚本继续。不过我想记录错误。有人能帮我弄清楚吗


试试看{
如果($id==4)
{
回声“测试”;
}
}
捕获(例外$e){
echo$e->getMessage();
}
echo‘你好,你应该来看我……’ 在调用可能抛出do的代码的代码中

输出将为(为可读性添加换行符):

Try/Catch块也可以嵌套。请参见上面链接的PHP手册页面中的示例2:

try{
    try {
        throw new Exception('Foo');
        echo 'not getting here';
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    echo 'bar';
} catch (Exception $e) {
    echo $e->getMessage();
}
echo 'done';

'Foo'  // echoed in inner catch block
'bar'  // echoed after inner try/catch block
'done' // echoed after outer try/catch block

在DevZone进一步阅读:


    • 您必须捕获异常:

      // some code
      
      try {
          // some code, that might throw an exception
          // Note that, when the exception is thrown, the code that's after what
          // threw it, until the end of this "try" block, will not be executed
      } catch (Exception $e) {
          // deal with the exception
          // code that will be executed only when an exception is thrown
          echo $e->getMessage(); // for instance
      }
      
      // some code, that will always be executed
      

      以下是一些你应该阅读的内容:


      try语句中的脚本将不会继续执行。非常好,戈登,谢谢。我已经有这么多了。但是,当出现错误(TCP/IP)时,脚本将终止。我想做的是,因为这些TCP/IP错误通常会很快纠正,只需让脚本继续。但是,我想记录错误。使用异常是否可能发生这种情况?我不确定我是否理解您所指的TCP/IP错误。如果您的意思是与服务器的连接已断开,并且您希望在下一个请求时重新连接,则不会。这是不可能的。如果连接在第一次超时时断开,请记录错误,但继续。如果它再次失败,它就会死亡。可能吗?谢谢你,戈登。这很有帮助。这看起来也像我在顶部发布的内容,我试图在第二个错误上抛出异常。但这看起来不可能。try语句中的代码将不会继续执行。@Chacha102:我编辑了我的答案以坚持这一点——解决方案是将代码“必须始终执行”放在try/catch块之后;;不过,这提供了记录异常的能力,脚本将继续。谢谢Pascal,我已经记下了很多。我想记录第一个错误,并让脚本继续,如果可能的话。“这可能吗?”帕斯卡,我相信你不明白这个问题。他希望try语句中的块继续执行。只要捕获到您的异常,并且您没有在catch块中使用exit not die(也没有任何引起另一个问题的内容),您的脚本就没有理由结束。。。您能否编辑您的问题,向我们展示更多相关的代码,以便我们更好地理解?您不能使用异常来执行此操作,因为它内置于代码中。但是,您可以使用自定义错误类,或者
      trigger\u error
      Thank Chacha。我将调查触发错误。不要相信我以前用过这个。只要快速浏览一下这本书中的文档。。。它是用户级的,除非我可以登录到一个文件而不用查看消息,否则它对我不起作用。您可以使用
      set_error_handler
      ()来处理由
      trigger_error
      ;触发的错误;;顺便说一句,在你发布的代码中,没有任何抛出和异常…@Bebo,你可能想尝试睡觉,然后再回来:)最烦人的事情之一是试图在没有任何睡眠的情况下编程。
      'Foo'                                         // echoed in try block
      'Nothing in this try block beyond this line'  // echoed in Exception catch block
      'done'                                        // echoed after try/catch block
      
      try{
          try {
              throw new Exception('Foo');
              echo 'not getting here';
          } catch (Exception $e) {
              echo $e->getMessage();
          }
          echo 'bar';
      } catch (Exception $e) {
          echo $e->getMessage();
      }
      echo 'done';
      
      'Foo'  // echoed in inner catch block
      'bar'  // echoed after inner try/catch block
      'done' // echoed after outer try/catch block
      
      // some code
      
      try {
          // some code, that might throw an exception
          // Note that, when the exception is thrown, the code that's after what
          // threw it, until the end of this "try" block, will not be executed
      } catch (Exception $e) {
          // deal with the exception
          // code that will be executed only when an exception is thrown
          echo $e->getMessage(); // for instance
      }
      
      // some code, that will always be executed