PHP5 exception handling mechanism [8] - use throw keyword

xiaoxiao2021-03-06  92

Use throw keywords

After establishing an Exception object, you can return objects, but should not be used like this, better ways to replace it with the THROW keyword. Throw is used to throw an exception:

Throw New Exception ("My Message", 44);

Throw will abort the script and make the relevant Exception objects available to the customer code.

The following is an improved getcommandObject () method:

Index_php5.php

cmddir} / {$ CMD} .php "; if (! file_exists ($ PATH)) {throw new exception;} Require_once $ path; if (! Class_exists ($ cmd)) {throw new Exception (" Class $ CMD Does Not Exist ");} $ CMD); if (! $ class-> issubclassof (New Reflection")) {throw new exception ("$ cmd is not a commnd") } RETURN NEW $ cmd ();}}?>

In the code, we used the reflection API to determine if 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 'Can 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, throw an exception causes one

Fatal Error

. This means that there is a security mechanism with an abnormal class. And only one error mark cannot have such a function. Processing error tag failed to continue using the error value of your script.

转载请注明原文地址:https://www.9cbs.com/read-121828.html

New Post(0)