passthru()、system()或exec()出现异常气泡


Exception Bubbling with passthru(), system(), or exec()

在我自己的测试中,我无法使用类似系统的命令来引发异常。只是好奇是否有办法实现这一点?

test1.php:

<?php
   try {
      // None of these (which throw an exception)
      // allow exception to be caught here
      passthru("php -f test2.php");
      system("php -f test2.php");
      exec("php -f test2.php");
   } catch (Exception $e) {
      echo( "Caught exception: " . $e->getMessage() );
   }
?>

test2.php:

<?php
   // Throw an exception
   throw new Exception();       
?>

我知道它可以被包含捕获,但出于我的目的,我不能使用它。当以这种方式执行脚本时,有没有办法捕捉或"弹出"异常?

正如@minitech所说,您必须收听STDERR

当您通过system()访问命令调用PHP脚本时,它会将它们作为一个单独的进程运行,而您的调用脚本down无法访问该进程。

如果您想以这种方式进行操作,您可以查看POSIX扩展。

如果没有,您将不得不测试system()调用的响应,而不是试图捕获Exception

不,您能做的最好的事情就是读取STDERR。php只能输出文本。