In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to achieve exception handling in php. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Detailed explanation of php exception handling
Function: centralized and unified handling of exceptions that occur in the code. An exception is thrown directly in the code block, and the code block does not handle the exception, but deals with the exception together.
Keywords in php exception handling:
Try: monitoring code block
Catch: catching exceptions
Throw: throwing an exception
Finally: it is executed regardless of whether there is an exception or not, and can be omitted.
Exception: exception class
Exception handling syntax structure: try {/ / detect code} catch (Exception $ex) {/ / catch exception} finally {/ / execute regardless of whether there is an exception or not, and finally can be omitted. } sample code: if (isset ($_ POST ['button'])) {try {$age = $_ POST [' age'] If ($age=='') {throw new Exception ('age cannot be empty', 1001) / / throw exception} if (! is_numeric ($age)) {throw new Exception ('age must be numeric' 1002) / / throw exception} if (($age=125) {throw new Exception ('age must be 1-125 years old 1003) / / throw exception} catch (Exception $ex) {/ / catch exception echo' error message'. $ex- > getMessage () Echo 'error code'. $ex- > getCode (); echo 'file address'. $ex- > getFile (); echo 'error line number'. $ex- > getLine ();} finally {echo 'close database connection'; / / execute regardless of whether there is an exception or not, and finally can be omitted. }}
Note: after an exception is thrown, the execution of the try block is terminated and the execution permission is handed over to the catch block.
This is the end of the article on "how to implement exception handling in php". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.