PHP5 exception handling mechanism [12] - abnormal transmission, rehabilitation

xiaoxiao2021-03-06  83

If we have triggered an exception that can't be handled immediately, there is a good solution - check the responsibility of the abnormality back to the code to call the current method, that is, throw an exception again in the catch statement (turn throw abnormal). This will transfer an exception to the call chain of the method.

INDEX_PHP5_5.PHP

request = $ Request_Array)) {$ this-> Request = $ _ request;}} Function getcommandstring () {return ($ this-> cmdstr? $ this-> cmdstr: ($ this-> cmdstr = $ this-> request ['cmd ']));} Function Runcommand () {$ cmdstr = $ this-> getcommandstring (); try {$ mgr = new commandmanager (); $ cmd = $ mgr-> getcommandObject ($ cmdstr); $ cmd-> execute ();} Catch (IllegalCommandexception $ E) {Error_Log ($ E-> getMessage ()); if ($ cmdstr! = $ This-> defaultcmd) {$ this-> cmdstr = $ this-> defaultcmd; $ this- > Runcommand ();} else {throw $ e;}} catch (Exception $ e) {throw $ E;}}} $ helper = new r Equesthelper (Array (cmd => 'realcommand')); $ helper-> runcommand ();?>

We use a customer code in the Requesthelper class. Requesthelper is used to process request data from the user. In the constructor we accept an array for Debug. If this array is not accepted, the class will use a $ _Request array. Regardless of which array is used, it will be assigned to a variable named $ Request. Customer code tells what it wants to execute by giving a CMD element of a Request array. The getcommandstring () method tests an attribute called $ cmdstr. If it is empty, the method assigns the content of the CMD element in the $ request to $ cmdstr and return a value. If not empty, the method returns the value of the $ cmdstr property directly. With this mechanism, the Command string can be overwritten in the Requesthelper class.

At the end we will pass all anomaly objects outside IllegalCommandException to a higher level class to delay processing. We throw an exception again in the last CATCH statement. } catch (Exception $ E) {throw $ E;}

If we caught an IllegalCommandexception exception, we first try to call a default Command. By setting the $ cmdstr property to a $ defaultcmd equivalent and repeatedly calls the RunCommand method. If the $ cmdstr and $ defaultcmd strings have equal, we don't have anything to do, then throw anomalies.

In fact, in the Zend Engine II will automatically turn all unmatched exceptions, we can omit the last Catch statement. This is the last line of CommandManager :: getcommandObject ():

Return $ Class-> newinstance ();

I have to pay attention to two problems here:

First, we assume that the constructor of the CommandManager class does not require parameters. In this article, we do not discuss the case of the need for parameters.

Second, we assume

Command

class

(

Here is our customized

Realcommand

)

Can be instantiated. If the constructor is declared as

Private

This statement will throw a

REFLECTIONEXCEPTION

Object. If we are not in

Requesthelper

This exception will be delivered to calls in the process.

Requesthelper

Code in the code. If an exception is implicitly thrown, you'd better explain it in the document, or manually throw this exception.

-

This other programmers use your code to handle the possible abnormal conditions.

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

New Post(0)