如何在没有脚本死亡的情况下使用异常?

Beb*_*ebo 2 php

我想在我的代码的一部分中使用异常来处理错误,但如果代码失败,我希望脚本继续.我想记录错误.有人可以帮我解决这个问题吗?


try{
    if($id == 4)
    {
      echo'test';
    }
}
catch(Exception $e){
    echo $e->getMessage();
}

echo'Hello, you should see me...'; <------ I never see this.. No errors, just a trace.
Run Code Online (Sandbox Code Playgroud)

Pas*_*TIN 5

你必须抓住异常:

// 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
Run Code Online (Sandbox Code Playgroud)


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

  • 只要您的异常被捕获,并且您没有在catch块中使用exit而不会死(也没有任何引发其他问题),您的脚本没有理由结束...你可以编辑你的问题,以显示我们更相关的代码,所以我们可以更好地理解? (3认同)