PHP try, throw and catch simple usage _php tutorial

Source: Internet
Author: User

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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.