php5| Keyword | exception handling you can return an object after you have created a exception object, but you should not use it this way, and a better way is to use the Throw keyword instead. Throw to throw an exception:
throw new Exception ("My Message", 44);
Throw abort the execution of the script and make the associated exception object available to the client code.
The following is an improved Getcommandobject () method:
function Getcommandobject ($cmd) {
$path = "{$this->cmddir}/{$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 PHP5 reflection (Reflection) API to determine whether the given class belongs to the command type. Executing this script under the wrong path will report this error:
Fatal error:uncaught Exception ' exception ' with message ' cannot find command/xrealcommand.php ' In/home/xyz/basicexcepti On.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, throwing an exception causes a fatal error. This means that a security mechanism is built into the class that uses the exception. Instead of using just one error tag, you can't have such a feature. Failure to handle the error tag will only make your script use the wrong value to continue execution.
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.