Unlike Java, exceptions must be thrown manually in PHP.
Throw and catch an exception, example:
Exception ("A terrible error has occurred", 42);} catch (Exception $e) { echo "Exception ". $e->getcode (). ":". $e->getmessage (). "
"." In ". $e->getfile (). $e->getline ()."
";}
Show Results:
Exception Handling ">
Built-in methods for the Exception class:
GetCode ()--Returns the code passed to the constructor;
GetMessage ()--Returns the message passed to the paparazzi function;
GetFile ()--Returns the full path of the code file that generated the exception;
GetLine ()--Returns the code line number of the code file that generated the exception;
gettrance--returns an array containing the code fallback path that generated the exception;
gettranceasstring--returns a message with the same direction as the Gettrance (), which will be formatted with a string;
__tostring ()--allows a simple display of a Exception object and gives the information that all of the above methods can provide.
Examples of user-defined exceptions:
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;}
An application
Exception HandlingExample: File I/O processing
You first need to create a file for the exception class: File_Exception. php
Exception { function __tostring () { return "FileOpenException ". $this->getcode (). ":". $this GetMessage (). "
"." In ". $this->getfile (). $this->getline ()."
"; }} Custom cannot write exception class FileWriteExceptionException{ function __tostring () { return "FileWrite Exception ". $this->getcode ().": ". $this->getmessage ()."
"." In ". $this->getfile (). $this->getline ()."
"; }} Custom cannot get write lock exception class FilelockExceptionException{ function __tostring () { return ' FilelockException ". $this->getcode ().": ". $this->getmessage ()."
"." In ". $this->getfile (). $this->getline ()."
"; }}
Then introduce file in the main file processorder.php.
Exception. php Files
require_once ("File_Exception. php");
Exception HandlingKey code:
Set the file output content and format $out _put_string= $date. \ t ". $cloths." piece mens \ t ". $shoes." Two Shoes \ t ". $glasses." Pair of glasses \t\ Total Price: ¥ ". $total _amount." Delivery address: \ t ". $address." \ n "; exception handling try{ //Open file, (Append mode + binary mode) if (! ( $fp = @fopen ("$DOCUMENT _root/l02/files/orders.txt", ' AB '))) throw new FileOpenException(); Write Operation Lock if (!flock ($FP, LOCK_EX)) throw new FilelockException(); Writes data to the file if (!fwrite ($fp, $out _put_string,strlen ($out _put_string)) throw new FileWriteException (); Release existing lock flock ($fp, lock_un); Close file stream fclose ($fp); echo "Data Save complete
"; } catch (FileOpenException $foe) { echo "File open failed, please check the server is abnormal!
"; Exit; } catch (Exception $e) { echo 'Your order has not been submitted for completion, please try again.
"; Exit; }
The above describes the PHP error and exception handling, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.