PHP 5 exception handling mechanism using Throw keyword _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Use the Throw keyword for the exception handling mechanism of PHP5. After an Exception object is created, you can return the object, but you should not use it like this. a better way is to use the keyword throw instead. Throw is used to throw an Exception: thrownewException (after my creates an Exception object, you can return the object, but it should not be used like this. a better way is to use the keyword throw instead. Throw is used to throw an exception:

Throw new Exception ("my message", 44 );

Throw suspends the execution of the script and makes the Exception object available to the client code.

The getCommandObject () method is improved as follows:

Index_php5.php

// PHP 5
Require_once ('command _ php5/Command. php ');
Class CommandManager {
Private $ export dir = "pai_php5 ";

Function getCommandObject ($ cmd ){
$ Path = "{$ this-> Export Dir}/{$ cmd}. php ";
If (! File_exists ($ path )){
Throw new Exception ("Cannot find $ path ");
}
Require_once $ path;
If (! Class_exists ($ cmd )){
Throw new Exception ("class $ cmd does not exist ");
}

$ Class = new ReflectionClass ($ cmd );
If (! $ Class-> isSubclassOf (new ReflectionClass ('command '))){
Throw new Exception ("$ cmd is not a Command ");
}
Return new $ cmd ();
}
}
?>

In the code, we use the Reflection API of PHP5 to determine whether the given class belongs to the Command type. If you execute this script in the wrong path, the following error will be reported:

Fatal error: Uncaught exception 'exception' with message 'could not find command/xrealcommand. php' in/home/xyz/BasicException. php: 10
Stack trace:
#0/home/xyz/BasicException. php (26 ):
CommandManager-> getCommandObject ('xrealcommand ')
#1 {main}
Thrown in/home/xyz/BasicException. php on line 10

By default, a fatal error occurs when an exception is thrown. This means that a security mechanism is built in the abnormal class. Using only one error mark does not support this function. If the error Mark fails to be processed, your script will use the error value to continue execution.

Bytes. Throw is used to throw an Exception: throw new Exception (my...

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.