Home > Backend Development > PHP Tutorial > php syntax error capture

php syntax error capture

王林
Release: 2023-04-07 14:26:02
Original
3501 people have browsed it

php syntax error capture

PHP syntax error capture processing

Generally, the method used to capture errors is:

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

or

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

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();//参数错误
}
Copy after login

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

Recommended tutorial: PHP video tutorial

The above is the detailed content of php syntax error capture. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template