Home >Backend Development >PHP Problem >What is the method of catching php errors?

What is the method of catching php errors?

王林
王林Original
2020-11-09 10:05:441332browse

The method of catching php errors is: you can use the [try{ }catch(Exception $e){echo $e->getMessage();}] statement to catch errors.

What is the method of catching php errors?

The general method used to capture errors is:

(Learning video sharing: java video tutorial)

try{
  ...
}catch(Exception $e){
  echo $e->getMessage();
}

or

set_exception_handler(function ($exception) {
  echo $exception->getMessage();
});

Example:

<?php

function test(){
    throw new Exception(&#39;参数错误&#39;);
}

try{
    //如果catch没有捕获到,才会执行到这里
    set_exception_handler(function ($exception) {
        echo $exception;//exception &#39;Exception&#39; with message &#39;参数错误&#39; in /www/web/...(一堆信息)
        echo &#39;<br>&#39;;
        echo $exception->getMessage();//参数错误
    });

    test();
}catch(Exception $e){
    echo $e->getMessage();//参数错误
}

The set_exception_handler function sets a user-defined exception handling function for exceptions that are not caught with try/catch blocks.

Related recommendations: php training

The above is the detailed content of What is the method of catching php errors?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more