Php抛出异常致命错误,使用XDebug的Visual Studio代码中未捕获的异常

问题描述 投票:-1回答:1

我正在尝试在try ... catch块中引发异常,但是由于某种原因,我得到了一个错误致命错误:未捕获的异常。分配了处理程序,但是为什么不触发呢?

try {
   if (!isset($_SESSION["Authorized"])){
      throw new Exception();
   } 
} catch (Exception $e){
   die('No access');
}

澄清,我很惊讶地发现该错误仅在使用Xdebug调试Visual Studio代码时发生。如果关闭调试模式,则不会发生任何错误。为什么这样?

php exception
1个回答
-3
投票

参考如何使用throw:https://www.php.net/manual/en/internals2.opcodes.throw.php

try {
    $error = 'Always throw this error';
    throw new Exception($error);

    // Code following an exception is not executed.
    echo 'Never executed';

} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
© www.soinside.com 2019 - 2024. All rights reserved.