PHP try, throw and catch simple usage
This article simply records the use of Try, throw, and catch in PHP, with time to do a detailed parsing later.
Try-the function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. But if the exception is triggered, a
Abnormal.
Throw-this specifies how the exception is triggered. Each "throw" must correspond to at least one "catch"
Catch-the "catch" code block catches an exception and creates an object that contains the exception information
Let's trigger an exception:
1) {throw new Exception ("Value must be 1 or below");} return true;} The exception is triggered in the "try" code block Try{checknum (2);//Catch Exception}catch (Exception $e) {echo ' Message: '. $e->getmessage ();}
The above code will get an error like this:
Message:value must be 1 or below
Example Explanation:
The above code throws an exception and captures it:
Create the Checknum () function. It detects if the number is greater than 1. If it is, an exception is thrown.
Call the Checknum () function in the "Try" code block.
The exception in the Checknum () function is thrown
The catch code block receives the exception and creates an object ($e) that contains the exception information.
The error message from this exception is output by calling $e->getmessage () from this exception object, but in order to follow the "each throw must correspond to a
Catch principle, you can set up a top-level exception handler to handle missed errors.
Articles you may be interested in
- Simple example of PHP getting Web content via socket
- PHP tip php warning:date (): It is not safe to rely on the ... The wrong solution
- PHP $this, Static, final, const, self, and several other key words to use
- PhpMyAdmin cannot start session without errors error resolution
- Installation and configuration tutorial for memcache under Windows
- Send mail SMTP Error Could not connect to SMTP host. Solution for Send fail
- Comparison of performance efficiency of file_get_contents to curl in PHP
- The principles and tutorials of PHP connection, manipulation memcached
http://www.bkjia.com/PHPjc/987400.html www.bkjia.com true http://www.bkjia.com/PHPjc/987400.html techarticle PHP try, throw and catch simple usage this article simply records the use of try, throw, and catch in PHP, and then has time to do a detailed parsing later. Try-Use the exception letter ...