In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to understand exception handling in PHP". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to handle these situations! I hope you can read carefully and learn something!
PHP errors and exception handling is very common in PHP, in our daily development, will certainly encounter such as forget to add semicolon, function name written wrong or function has been redefined and so on a lot of errors, if in the development process, can find errors show errors, it is certainly very beneficial to our development.
Therefore, the proper use of a process while developing a project can help us find errors and correct them to speed up development. So let's take a look at how to understand our error handling.
Exception handling classes in PHP
PHP provides a built-in exception handling class, Exception, which contains exception handling functions that catch program exceptions and errors.
Here are some of the more common functions in this class:
getTraceAsString(): Returns information formatted as a string generated by the getTrace() function
__toString(): String information that generates an exception, which can be overloaded. Note that the function starts with two underscores
getMessage(): Returns the content of the exception message
getLine(): Returns the line number of the code where the error occurred
getCode(): Returns the exception code as a number
getFile(): Returns the name of the file where the exception occurred
getTrace(): Returns the backtrace() array
catch exceptions in programs
Exceptions in the program generally do not appear on their own, this time we can use the try catch statement and throw keyword to catch the purpose of the exception in the program.
A try catch statement is similar to a flow-controlled statement in that the throw keyword throws an exception, and we can catch exceptions in our programs by using structures like conditional selection. The syntax for a try catch statement is as follows:
try{ //exception or error code may occur, such as file operation, database operation, etc.}catch(Exception $a){ // $a is an exception class object //output error message}
When we need to catch program exceptions, we need to put the code to be caught into the try code block. In the above syntax, every try should have at least one catch corresponding to it. When there are no catch matching exceptions in the try block, the code jumps to the last catch and continues.
Any exception generated in it can be run by throw statements and caught by catch, and when an exception occurs, the code following it will not continue to execute.
Examples are as follows:
Output:
In the above example, try statement tries to determine whether there is a directory named demo under the current directory, directory does not exist, so execute throw keyword throw exception. After an exception is thrown, the rest of the try statement will not run again.
Create your own exception class
PHP can define some exceptions in advance, because PHP rarely actively throws exceptions, when the exception is defined in advance, we can judge the possible exceptions through if-else, manually throw exceptions, and then PHP often can use the exception class we created ourselves.
Examples are as follows:
In the example above, two exception classes are defined, both inheriting from the Exception base class.
In the actual business, we will also throw different exceptions according to different requirements. Examples are as follows:
function reg($reg) { if (empty($reg['email'])) { throw new emailException("emaill is null", 1); } if(empty($reg['name'])) { throw new nameException("name is null", 2); }}
When executing business code, you can use the if statement to determine where exceptions will occur, and then manually throw exceptions, and distribute different exceptions to different exception classes through statements; in the following example, catch different exceptions according to different situations. When the first catch catches an exception, even if there are still other exceptions in the program, it will skip other catch code blocks. The statement in the last finally will be executed regardless of whether an exception occurs in the program. Examples are as follows:
try{ $reg = array('phone'=>'1888888888'); reg($reg);} catch(emailException $e) { echo $e;} catch(nameException $e) { echo 'error msg:' .$ e->getMessage(). 'error code:'.$ e->getCode();} finally { echo ' finally';}"How to understand exception handling in PHP" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.