Using exceptions
PHP5 adds an exception-handling module similar to other languages. The exception generated in the PHP code can be thrown by the throw statement and caught by the catch statement. Code that requires exception handling must be placed inside the try code block to catch possible exceptions. Each try corresponds to at least one catch block. Use multiple catch to catch exceptions generated by different classes. When a try code block no longer throws an exception or cannot find a catch to match the thrown exception, the PHP code continues after the jump to the last catch. Of course, PHP allows you to throw (throw) exceptions again within a catch code block.
Predefined exception Exception
The Exception class is the base class for all exceptions, and we can derive the Exception class to achieve the purpose of the custom exception. The following list lists the basic information for Exception.
Copy Code code as follows:
Exception {
/* Properties * *
protected string $message; Exception message Contents
protected int $code; Exception code
protected string $file; Name of the exception thrown
protected int $line; The line number that throws the exception in the file
/* Method * *
Public __construct ([String $message = "[, int $code = 0 [, Exception $previous = NULL]])//Exception constructor
Final public string getMessage (void)//Get Exception message content
Final public Exception getprevious (void)//Return the previous exception in the chain of exceptions
Final public int getcode (void)//Get Exception code
Final public string getFile (void)//Get the name of the program file where the exception occurred
Final public int getline (void)//Get the line number of the code in the file where the exception occurred
Final public array gettrace (void)//Get exception tracking information
Final public string gettraceasstring (void)//Get exception tracking information of string type
public string __tostring (void)//Converting an exception object to a string
Final private void __clone (void)//Exception clone
}
After we've learned Exception, we'll try to extend the Exception class to implement a custom exception.
Copy Code code as follows:
function Connecttodatabase ()
{
if (! $link = mysql_connect ("Myhost", "MyUser", "MYPASSW", "MYBD"))
{
throw new Exception ("Could not connect to the database");
}
}
Try
{
Connecttodatabase ();
}
catch (Exception $e)
{echo $e->getmessage ();
}
Here we throw the Class A Exception type of exception, and catch the exception in the catch, and finally print out the "could not connect to the" database. Maybe you want to also show the reason information for the database connection failure. Below, and by extending the exception class to implement our custom information.
Copy Code code as follows:
Class MyException extends Exception
{
protected $ErrorInfo;
The constructor handles some logic and then passes some information to the base class
Public function __construct ($message =null, $code =0)
{
$this->errorinfo = ' Custom error message for wrong class ';
Parent::__construct ($message, $code);
}
Provides methods to get custom class information
Public Function GetErrorInfo ()
{
return $this->errorinfo;
}
/**
*
* You can also add an exception log, just call it in the constructor above.
*
*/
Public function log ($file)
{
File_put_contents ($fiel, $this->__tostring (), file_append);
}
}
function Connecttodatabase ()
{
throw new MyException ("errormessage");
}
Try
{
Connecttodatabase ();
}
catch (MyException $e)
{
echo $e->getmessage (). "\ n";
echo $e->geterrorinfo ();
}
Set_exception_handler set a user-defined exception handler function
The function name called when an Set_exception_handler exception occurs is used as a parameter to the argument. The function must be defined before calling Set_exception_handler (). The function takes a parameter, which is an exception object thrown. This can be used to improve the Exception log processing mentioned above.
Copy Code code as follows:
function Exceptionlogger ($exception)
{
$file = ' ExceptionLog.log ';
File_put_contents ($fiel, $exception->__tostring (), file_append);
}
Set_exception_handler (Exceptionlogger);
1.3. PHP allows you to throw (throw) exceptions again within the catch code block.
Copy Code code as follows:
Try
{
#code ...
}
catch (Exception $e)
{
if ($e->getcode () = = 999)
{
#进行一些操作
}
Else
{
Throw $e;
}
}
Summarize
Exceptions are very powerful, but not in the form of indiscriminate misuse of exception mechanisms in our projects, especially the extensive use of exception logs, which greatly increases system overhead and reduces application performance. With error code we can easily manage the error information, when an error message is repeatedly thrown out, the use of error code is a scientific choice. We can even use error codes to make error messages support multiple language displays.