The depth exploration of PHP 5.0 exception handling mechanism

Source: Internet
Author: User
Tags array error code error handling exception handling execution getmessage php basics stack trace
Exception handling This article is intended for programmers who want to learn about the PHP5 exception handling mechanism. Read this article you need to have some object-oriented programming and PHP basics.

The following member methods are required for exception classes built in PHP5:

The __construct () constructor requires an error message and an optional integer error mark as a parameter getmessage () to obtain an error message GetCode ()
The error code getFile () the number of rows in which the file Getline () exception occurred gettrace () tracks the route that was passed for each step of the exception, stored in an array, and returned the array gettraceasstring () and Gettrace () functions, However, you can turn elements in an array into strings and output in a certain format
As you can see, the structure of the Exception class is very similar to the pear_error. When you encounter an error in your script, you can create your exception object:

$ex = new Exception ("Could not open $this->file");
The constructor of the exception class will accept an error message and an error code.

   using the Throw keyword

After you create a exception object, you can return the object, but this should not be used, and the 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:

index_php5.php

<?php//PHP 5
Require_once (' cmd_php5/command.php ');
Class Commandmanager {
Private $cmdDir = "CMD_PHP5";

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.

   Try-catch Statement

To further handle the exception, we need to use the Try-catch statement-including the try statement and at least one of the catch statements. Any code that invokes a method that might throw an exception should use a try statement. Catch statements are used to handle exceptions that may be thrown. Here's how we handle the exception thrown by Getcommandobject ():

Index_php5.php the second half of the block

<?php
PHP 5
try {
$mgr = new Commandmanager ();
$cmd = $mgr->getcommandobject (' Realcommand ');
$cmd->execute ();
catch (Exception $e) {
Print $e->getmessage ();
Exit ();
}
? >


As you can see, by using the Throw keyword and the try-catch statement together, we can avoid the error marking the value returned by the pollution class method. Because "exception" itself is a type of PHP that is different from any other object, it does not create confusion.

If an exception is thrown, the script in the Try statement stops executing, and then immediately moves to the script in the catch statement.

If the exception is thrown and is not captured, a fatal error is generated.

Handling Multiple errors

Exception handling so far seems to be the traditional practice of verifying that the returned error identity or the value of an object is not much different. Let's be more cautious with commandmanager processing and check the command directory for existence in the constructor.

index_php5_2.php

//php 5
require_once (' cmd_php5/command.php ');
Class Commandmanager {
Private $cmdDir = "CMD_PHP5";

Function __construct () {
if (!is_dir ($this->cmddir)) {
throw new Exception ("Directory Error: $this-" Cmddir ");
}
}

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 ();
}
}
? >


There are two places where calls can cause program errors (__construct () and Getcommandobject ()). Still, we don't need to adjust our client code. You can add a lot of content to the try statement and then treat it uniformly in the catch. If the constructor of the Commandmanager object throws an exception, execution aborts in the Try statement, and then the catch statement is invoked to catch the associated exception. Similarly, Getcommandobject () is also true. In this way, we have two potential error-causing errors, and a unique statement to handle all the errors. This makes our code look neater and can meet the requirements of error handling. Compared to the traditional error methods of PHP mentioned earlier, there is a clear advantage.

Index_php5_2.php the second half of the block

Note: Although the first half of the code has two possible errors compared to index_php5.php, the code is exactly the same as the second half of the index_php5.php.

PHP 5
try {
$mgr = new Commandmanager (); Potential error
$cmd = $mgr->getcommandobject (' Realcommand ');
Another potential error
$cmd->execute ();
catch (Exception $e) {
Handle either error here
Print $e->getmessage ();
Exit ();
}
? >


There's one more place we haven't mentioned. How do we distinguish between different types of errors? For example, we might want to use one method to handle an error that cannot find a directory, and another way to handle an illegal command class.

The exception class can accept an optional integer error identification, which is a way to distinguish between different types of errors in a catch statement.

index_php5_3.php

<?php
PHP 5
Require_once (' cmd_php5/command.php ');
Class Commandmanager {
Private $cmdDir = "CMD_PHP5";
Const CMDMAN_GENERAL_ERROR = 1;
Const CMDMAN_ILLEGALCLASS_ERROR = 2;

function __construct () {
if (!is_dir ($this->cmddir)) {
throw new Exception ("Directory Error: $this->cmddir", self::cmdman_general_error);
}
}

function Getcommandobject ($cmd) {
$path = "{$this->cmddir}/{$cmd}.php";
if (!file_exists ($path)) {
throw new Exception ("Cannot find $path", self::cmdman_illegalclass_error);
}
Require_once $path;
if (!class_exists ($cmd)) {
throw New Exception ("Class $cmd does not exist", self::cmdman_illegalclass_error);
}

$class = new Reflectionclass ($cmd);
if (! $class->issubclassof (New Reflectionclass (' Command ')) {
throw new Exception ("$cmd is not a Command", self::cmdman_illegalclass_error);
}
return $class->newinstance ();
}
}
? >


By passing Cmdman_illegalclass_error and cmdman_general_error one of the parameters to the exception object we throw, we can get the client code to differentiate between different types of errors and define different processing strategies.

index_php5_3.php

try {
$mgr = new Commandmanager ();
$cmd = $mgr->getcommandobject (' Realcommand ');
$cmd->execute ();
catch (Exception $e) {
if ($e->getcode () = = Commandmanager::cmdman_general_error) {
No way of recovering
Die ($e->getmessage ());
else if ($e->getcode () = = Commandmanager::cmdman_illegalclass_error) {
Error_log ($e->getmessage ());
Print "Attempting recovery\n";
Perhaps attempt to invoke a default command?
}
}
? >


We can also implement this effect in another way-derive from the most fundamental exception class of subclasses that represent different types of exceptions, and then throw and catch.

[1] [2] Next page



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.