PHP Exception detailed _php tutorial

Source: Internet
Author: User
Tags stack trace
What is the exception to PHP?
PHP 5 provides a new approach to object-oriented error handling. Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This condition is called an exception.

General methods of Use:
[PHP]
function Test () {
throw new Exception ("abnormal");
}
try {
Test ();
} catch (Exception $e) {
echo $e->getmessage ();
}
throw new Exception (' XXX '): Throws an exception
Try: The function that uses the exception should be in the "try" code block. If no exception is triggered, the code will continue to execute as usual. However, if an exception is triggered, an exception is thrown.
Catch: A block of code catches an exception and creates an object that contains the exception information
Custom Exception classes:
[PHP]
Class MyException extends Exception {
Public Function errormessage () {
$ERRORMSG = ' Error on line '. $this->getline (). ' In '. $this->getfile ()
.': '. $this->getmessage (). 'is not a valid e-mail address ';
return $ERRORMSG;
}
}


try {
throw new MyException ($email);
}catch (MyException $e) {
echo $e->errormessage ();
}
The MyException class is a custom exception class that must inherit exception
Exception is a PHP built-in exception class that uses PHP's default built-in exception class by default, but we can customize the exception class to meet our own needs in a custom way.
When you use a custom exception class, throw new MyException throws an exception and also throws a custom exception class class name
Catch is also required to capture custom exception classes
If an exception is thrown without capturing processing, a PHP code error will occur, as follows:
[SQL]
Fatal error:uncaught exception ' MyException ' in D:\appserv\www\cctv\trunk\index.php:12 Stack trace: #0 {main}
Thrown in D:\AppServ\www\cctv\trunk\index.php on line 12

PHP Default Exception class verbose:
[PHP]
Class exception
{
Protected $message = ' Unknow exception ';//Custom exception information
Protected $code = 0; Defined exception code
protected $file;//PHP program name with exception
protected $line;//php line number with exception
//constructor for passing user custom exception information and user-defined exception code
function __construct ($message =null, $code =0);
Final function getMessage ();
Final function GetCode ();
Final function getFile ();
Final function getLine ();
Final function gettrace ();//Returns the route of the exception pass as an array
Final function gettraceasstring ();//returns GETTRACE function information formatted as a string
Fu Nction __tostring (); can be overloaded to return a string that can be output
}
You can see that there are many custom variables and methods in the PHP default exception class
$e->getmessage is generally getting error messages
$e->getcode () is generally getting the error code
$e->getfile () Gets the wrong file information
$e->getline () Gets the number of error rows
$e->gettrace () exception passes the file path
__tostring () can generally be overloaded, Used to return an output-capable string
The role of the PHP exception class:
Catch exceptions, which can generally be thrown at the top of the code when the MySQL link is not available, or when the business logic is wrong.
Exception parsing processing. You can catch an exception error message and write to the log at the time of the exception. The
can return friendly hints, such as the web side can return a page form, or the JSON data format can be returned on the Ajax side.
Principle: If an exception is thrown, it must be caught, or PHP will get an error.

Author: initphp

http://www.bkjia.com/PHPjc/478076.html www.bkjia.com true http://www.bkjia.com/PHPjc/478076.html techarticle what is the exception to PHP? PHP 5 provides a new approach to object-oriented error handling. Exception handling is used to change the normal process of a script when a specified error (exception) condition occurs. This ...

  • 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.