There are many error handling methods in php, especially after php5, special php processing classes are provided. Below I have collected some methods and programs about PHP error handling to share with you.
Judge directly in the program
Basic error handling: use the die() function
The first example shows a simple script to open a text file:
The code is as follows | Copy code | ||||
|
Warning: fopen(welcome.txt) [function.fopen]: failed to open stream:
No such file or directory in C:webfoldertest.php on line 2代码如下 | 复制代码 |
//处理:判断文件是否存在 file_exists } echo "OK"; //第一种:使用简单的die语句 /* if(!file_exists("aa.txt")){ } echo "OK";
|
The code is as follows | Copy code |
<🎜> //Open a file without any processing<🎜> //$fp = fopen("aa.txt","r");<🎜> //echo "OK";<🎜><🎜> //Processing: Determine whether the file exists file_exists<🎜>/*<🎜> if(!file_exists ("aa.txt")){<🎜> echo "The file does not exist";<🎜> //Exit if it does not exist<🎜> exit(); //After exiting, the following code will not be executed<🎜 > }else{<🎜> $fp =fopen("aa.txt","r");<🎜> }<🎜><🎜> echo "OK";<🎜>*/<🎜> //3 ways to handle errors in PHP <🎜><🎜> //The first one: use a simple die statement <🎜>< 🎜>/* if(!file_exists("aa.txt")){<🎜> <🎜> die("The file does not exist. . . "); //If it does not exist, exit directly <🎜> }else{<🎜> $fp =fopen("aa.txt","r");<🎜> fclose($fp);<🎜><🎜> }<🎜><🎜> echo "OK";<🎜>*/<🎜> //Simpler way<🎜> file_exists("aa.txt") or die("File does not exist");<🎜><🎜><🎜>?> |
Second type: Error handler error level handling error method
The code is as follows | Copy code | ||||||||||||||||||||||||||||
error_file,error_line,error_context) The function must be able to handle at least two parameters (error level and error message), but can accept up to five parameters (optional: file, line -number and error context):
The above code will get an error like this: Fatal error: Uncaught exception 'Exception' Handling handlers should include: 1.Try - Functions that use exceptions should be inside a "try" block. If no exception is triggered, the code continues execution as usual. But if an exception is triggered, an exception will be thrown.
//If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below'; } Message: Value must be 1 or belowCreate a custom Exception ClassCreating custom exception handlers is very simple. We simply created a specialized class whose functions are called when an exception occurs in PHP. This class must be an extension of the exception class. This custom exception class inherits all properties of PHP's exception class, and you can add custom functions to it. We start to create the exception class:
Previous article:Detailed explanation of how to read large files in PHP_PHP Tutorial
Next article:Introduction to Taobao API component upgrade and adjustment methods_PHP tutorial
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 Articles by Author
Latest Issues
Group MySQL results by ID for looping over
I have a table with flight data in mysql. I'm writing a php code that will group and displ...
From 2024-04-06 17:27:56
0
1
406
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|