PHP5 exception handling mechanism [11] - subclass of Exception class

xiaoxiao2021-03-06  86

Subclass of the Exception class

There are two reasons to let us want to derive neutron classes from the Exception class:

1. Let subclasses provide custom features;

2. Different types of abnormalities;

Look at the second example. When using the CommandManager class, we may generate two errors: one is a general error. If the directory is not found, the other is not found or unable to generate a Command object. This way we need to define two exception subtypes for these two errors.

INDEX_PHP5_4.PHP

CMDDIR)) {Throw New CommandManageRexception ("Directory Error: $ this-> cmddir");}}} Function getcommandObject ($ cmd) {$ Path = "{$ this-> cmddir} / {$ cmd} .php "; if {throw new IllegalCommandException (file_exists ($ path)!) (" Can not find $ path ");} require_once $ path; (! class_exists ($ cmd)) if {throw new IllegalCommandException (" class $ cmd does NOT EXIST ");} $ CLASS = New ReflectionClass ($ CMD); if (! $ Class-> Issubclassof (New Reflection")) {throw new illegalcommandexception ("$ cmd is not a command");} Return $ Class-> newinstance ();}}?>

When our class can't find the correct Command directory, a CommandManageRexception will throw an exception; when an error occurs when generating a Command object, the getcommandObject () method will throw an IllegalCommandException. Note that there are several reasons that may result in throwing IllegalCommandException (if no file is found, or not found in the file). We combine the first two examples and provide intellectual error identification constants for IllegalCommandException to represent different types of errors.

The COMMANDMANAGER class now has the ability to handle these various error, we can add new Catch statements to match different error types.

INDEX_PHP5_4.PHP latter half

getcommandObject ('realcommand'); $ cmd-> execute ();} catch (commandmanagerexception $ e) {die ($ E-> getMessage ();} catch (IllegalCommandexception $ E) {Error_Log ($ E-> getMessage ()); Print "Attempting Recovery / N"; // Perhaps Attempt to Invoke A Default Command?} catch (Exception $ e) {Print "Unexpected Exception / N"; DIE ($ E-> getMessage ());}?> If the CommandManager object throws a CommandManageRexception exception, the corresponding CATCH statement will be executed. The parameters of each Catch statement are like a matching test, and the first matching catch statement will execute without performing other catch statements. So, you should write a catch statement for a specific exception, and write it back in the same CATCH statement for the general.

If you write a catch statement:

getcommandObject ('realcommand'); $ cmd-> execute ();} catch (Exception $ e) {print "unExpected Exception / N "; DIE ($ E-> getMessage ());} catch ($ e-> getMessage ());} catch (IllegalCommandexception $ E) {Error_Log ($ E-> GetMessage ()); Print "Attempting Recovery / N"; // Perhaps Attempt to Invoke A Default Command?}?>

Then when an exception is thrown, no matter what the first CATCH statement catch (Exception $ E) {} will always be executed. This is because any exception is from an Exception type, so it always matches. This does not meet the purpose of different processing we have to do with specific exceptions.

If you capture a specific type of exception, the exception of capturing the Exception type in the last catch statement is a good idea. The last Catch statement represents catch-all, capturing all exceptions. Of course, you may not want to handle an exception immediately, but want to pass it, then process it properly. This is another place in PHP's abnormal mechanism that needs to be discussed.

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

New Post(0)