Read the collection of PHP and MySQL Web development notes:
1. Exception Handling Concept
1) exception handling is invoked in a try code block
Try
{
Code goes here
}
2) in PHP, exceptions must be manually thrown
throw new Exception (' message ', code);
This is a language structure, not a function.
You can pass any other object in the throw clause.
3) After the try code block, you must give at least one catch code block.
catch (Typehint exception)
{
Handle exception
}
You can associate multiple catch code blocks 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) Brief introduction Exception is the base class for all exceptions. Class Summary Exception { /* Properties */ 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) } Property MessageException message Content CodeException code FileThe file name that throws the exception LineThrows the line number of the exception in the file Table of Contents
- Exception::__construct-Exception Constructors
- exception::getmessage-getting exception message content
- exception::getprevious-returns the previous exception in the exception chain
- exception::getcode-Getting exception codes
- Exception::getfile-Gets the program file name of the exception that occurred
- Exception::getline-gets the line number of the code in the file where the exception occurred
- exception::gettrace-getting exception tracking information
- exception::gettraceasstring-getting exception tracking information for a string type
- exception::__tostring-to convert an exception object to a string
- exception::__clone-Abnormal cloning
|
3. User-defined exception
Users can extend the exception class to create their own exception classes.
Note that most public methods of the exception class are final and cannot be overloaded, and we can create our own exception subclasses, but we cannot change the behavior of these basic methods. Note, however, that the __tostring () function can be overloaded.
Example:
Class MyException extends Exception { function __tostring () { Return "
Exception ". $this->getcode ()."
: ". $this->getmessage ()." "." In ". $this->getfile (). $this->getline ()." |
";} }
Try { throw new MyException ("A terrible error has occurred", 42); } catch (MyException $m) { Echo $m; }
?>
|
4. I/O Parts and recommendations
The I/O section is prone to exceptions, and generally good coding practices require less code in the try code block and catch related exceptions at the end of the code block.
Note: If a catch statement block is not matched, PHP will report 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 " The Orders file could not being opened. Webmaster for help. "; } catch (Exception $e) { echo " Your order could not being processed at this time. Please try again later. "; } |
5. Exception and PHP other error handling mechanism
PHP also provides a complex error handling mechanism, noting that exceptions such as exception handling and @ Error suppression are not affected.
Example:
if (! ( $fp = @fopen ("$DOCUMENT _root/. /orders/orders.txt ", ' AB '))) throw new Fileopenexception ();
|
If the function call fails, PHP will issue a dip to make an error report or record according to the php.ini settings.