Home  >  Article  >  Backend Development  >  How to catch errors and display friendly information in PHP_PHP Tutorial

How to catch errors and display friendly information in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 14:51:501064browse

To catch errors, you cannot use try{...}catch(){}. The try{...}catch in PHP needs to throw an exception yourself to catch it, which is different from other languages.

Secondly, you can use the following method to achieve the same effect:

error_reporting(0);//Set to block system error prompts and put them at the top of the page

//$string = file_get_contents("index.html");//Normal code
echo 5/0;//Change: 5/8 //Normal code

//If there is an error in the normal code, handle it:
$arr=error_get_last();//Get the error information that just occurred and return an array. If there is no error, return null.
if(isset($arr) ) //If it is not null, it means something went wrong
{
echo "An error occurred, error message:";
print_r($arr); //Specific error information can be modified as needed.
exit;
}

-------------------------------------------------- ---

【echo 5/8;】Output:

0.625


【echo 5/0;】Output:

An error occurred, error message:
Array (

[type] => 2

[message] => Division by zero

[file] => D:wampwwwsinaeditornewfile.php

[line] => 13

)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371782.htmlTechArticleTo catch errors, you cannot use try{...}catch(){}, try{ in PHP ...}catch requires that you throw an exception yourself to catch it, which is different from other languages. Secondly, you can use the following method...
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