异常会终止程序吗?

Does Exception Terminate Program?

考虑以下代码,运行 由 cron:

try {
    $count = $stmt->execute ( $queryArray );
}
catch ( PDOException $ex ) {
    fwrite ( $fp, '  "exception" at line: ' . (__LINE__ - 3). ",  " . $ex -> getMessage () . "\n" );
    throw new RuntimeException (
         basename (__FILE__) . '::' . __METHOD__ . ' - Execute query failed: ' . $ex -> getMessage ()  );
}

是否throw new RuntimeException重新抛出导致程序停止?换句话说,catch & fwrite 语句是否足以 'catch' 异常并允许程序继续?

throw 文档含糊不清。唯一的参考是 PHP Fatal Error 来自 (link to) PHP Exceptions:

When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." message, unless a handler has been defined with set_exception_handler().

Is re-throwing by throw new RuntimeException causing the program to stop?

是的,因为根据文档没有匹配的捕获。为了继续执行,您需要使用嵌套 try/catch catch 第二个异常 (RuntimeException),这通常不是一个好主意:

try {
   // something with PDO that generates an exception
} catch (PDOException $e) {
   // do some stuff
   try {
       throw new RuntimeException();
   } catch (RuntimeException()) {
     // do something else
   }
}

In other words, would the catch & fwrite statement sufficiently 'catch' the exception and allow the program to continue?

如果您想继续执行该程序,则需要不抛出第二个异常。但是,在您抛出该异常之前发生的任何事情都会发生。所以在你的例子中 fwrite 仍然会发生,当遇到 RuntimeException 时程序会停止。