PHP Error handling: die() Vs trigger_error() Vs throw Exception

前端 未结 2 1087
长情又很酷
长情又很酷 2020-12-02 04:49

In regards to Error handling in PHP -- As far I know there are 3 styles:

  1. die()or exit() style:

    $con = mysql_connec         
    
    
            
2条回答
  •  孤街浪徒
    2020-12-02 04:58

    I usually use the first way for simple debugging in development code. It is not recommended for production. The best way is to throw an exception, which you can catch in other parts of the program and do some error handling on.

    The three styles are not drop-in replacements for each other. The first one is not an error at all, but just a way to stop the script and output some debugging info for you to manually parse. The second one is not an error per se, but will be converted into an error if you don't catch it. The last one is triggering a real error in the PHP engine which will be handled according to the configuration of your PHP environment (in some cases shown to the user, in other cases just logged to a file or not saved at all).

提交回复
热议问题