PHP Study Notes 7: handling errors and exceptions read the notes in PHP and MySQL Web Development:
1. exception handling concept
1) exception handling is called and executed in the try code block
Try
{
// Code goes here
}
2) in PHP, an exception must be thrown manually.
Throw new Exception ('message', code );
This is a language structure, not a function.
Any other object can be passed in the throw clause.
3) after the try code block, at least one catch code block must be provided.
Catch (typehint exception)
{
// Handle exception
}
Multiple catch code blocks can be associated with a try code block.
Example:
Try { Throw new Exception ("A terrible error has occurred", 42 ); } Catch (Exception $ e ){ Echo "Exception". $ e-> getCode (). ":". $ e-> getMessage ()." ". "In". $ e-> getFile (). "on line". $ e-> getLine ()." "; } ?> |
2. Exception class
Exception (PHP 5> = 5.1.0) Introduction ExceptionIs the base class of all exceptions. Class abstract Exception{ /* Attribute */ Protectedstring $ message; Protectedint $ code; Protectedstring $ file; Protectedint $ line; /* Method */ Public _ construct ([string $ message = "" [, int $ code = 0 [, Exception $ previous =NULL]) Final public string getMessage (void) Final public Exception getPrevious (void) Final public int getCode (void) Final public string getFile (void) Final public int getLine (void) Final public array getTrace (void) Final public string getTraceAsString (void) Public string _ toString (void) Final private void _ clone (void) } Attribute message Exception message content CodeException code FileFile name that throws an exception LineThe row number in the file that throws an exception Table of Contents
- Exception: :__ construct-Exception constructor
- Exception: getMessage-get Exception message content
- Exception: getPrevious-returns the previous Exception in the Exception chain.
- Exception: getCode-get Exception code
- Exception: getFile-get the program file name with an Exception
- Exception: getLine-get the row number of the code with an Exception in the file
- Exception: getTrace-get Exception tracing information
- Exception: getTraceAsString-obtains the Exception tracking information of the string type.
- Exception ::__ toString-convert the Exception object to a string
- Exception :__ clone-abnormal clone
|
3. user-defined exceptions
You can extend the Exception class to create your own Exception class.
Note: Most public methods of the Exception class are final and cannot be overloaded. we can create our own Exception subclass, but we cannot change the behavior of these basic methods. However, note that the __tostring () function can be reloaded.
Example:
Class myException extends Exception { Function _ toString () { Return"
Exception ". $ this-> getCode ()." : ". $ This-> getMessage ()." "." In ". $ this-> getFile ()." on line ". $ this-> getLine ()." |
"; } } Try { Throw new myException ("A terrible error has occurred", 42 ); } Catch (myException $ m) { Echo $ m; } ?> |
4. I/O and suggestions
I/O is prone to exceptions. Generally, good coding habits require a small amount of try code blocks, and relevant exceptions are captured at the end of the code blocks.
Note: If a catch block does not match, PHP reports a fatal error.
Example:
// Open file for appending Try { If (! ($ Fp = @ fopen ("$ DOCUMENT_ROOT/../orders/orders.txt", 'AB '))) Throw new fileOpenException (); If (! Flock ($ fp, LOCK_EX )) Throw new fileLockException (); If (! Fwrite ($ fp, $ outputstring, strlen ($ outputstring ))) Throw new fileWriteException (); Flock ($ fp, LOCK_UN ); Fclose ($ fp ); Echo" Order written. "; } Catch (fileOpenException $ foe) { Echo" Orders file cocould not be opened. Please contact our webmaster for help. "; } Catch (Exception $ e) { Echo" Your order cocould not be processed at this time. Please try again later. "; } |
5. exception handling and other PHP error handling mechanisms
PHP also provides a complex error handling mechanism. Note that, for example, exception handling and @ error suppression will not affect the error.
Example:
If (! ($ Fp = @ fopen ("$ DOCUMENT_ROOT/../orders/orders.txt", 'AB '))) Throw new fileOpenException (); |
If this function fails to be called, PHP sends an extract and reports or records errors based on the settings of php. ini.