PHP Try-catch Statement usage tips, try-catch tips _php Tutorials

Source: Internet
Author: User
Tags php try catch try catch

PHP Try-catch Statement Usage tips, Try-catch use tips


PHP try-catch Statements
To further handle the exception, we need to use the Try-catch statement----include a try statement and at least one catch statement. Any code that calls a method that might throw an exception should use a try statement. The catch statement is used to handle the exceptions that may be thrown. The following shows the way we handle exceptions thrown by Getcommandobject ():

<?php try {   $mgr = new Commandmanager ();   $cmd = $mgr->getcommandobject ("Realcommand");   $cmd->execute (); } catch (Exception $e) {   print $e->getmessage ();   Exit (); }?>

As you can see, by using the Throw keyword together with the Try-catch statement, we can avoid the value returned by the error flag "contaminated" class method. Because "exception" itself is a type of PHP built-in that differs from any other object, it does not create confusion.

If an exception is thrown, the script in the Try statement stops executing and then immediately turns to the script in the Execute Catch statement.

If the exception is thrown and is not captured, a fatal error is generated.

Articles you may be interested in:

    • PHP Try catch Exception test
    • Example of a try catch catch exception in PHP

http://www.bkjia.com/PHPjc/1104326.html www.bkjia.com true http://www.bkjia.com/PHPjc/1104326.html techarticle PHP try-catch statement using tips, try-catch using the tip PHP Try-catch statement in order to further handle the exception, we need to use the Try-catch statement----including the Try statement and at least one CA ...

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