PHP exception details

Source: Internet
Author: User
What are PHP exceptions? PHP5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception. General usage: [php] & lt ;? Phpfunctiontest () {thrown... SyntaxHighlighter

What are PHP exceptions?
PHP 5 provides a new object-oriented error handling method. Exception handling is used to change the normal process of the script when a specified error (exception) occurs. This is called an exception.
 
General usage:
[Php]
Function test (){
Throw new Exception ("Exception ");
}
Try {
Test ();
} Catch (Exception $ e ){
Echo $ e-> getMessage ();
}
Throw new Exception ('XXX'): throws an Exception.
Try: the abnormal function should be located in the "try" code block. If no exception is triggered, the code continues as usual. However, if an exception is triggered, an exception is thrown.
Catch: the code block captures exceptions and creates an object containing exception information.
Custom exception class:
[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 and must inherit Exception
Exception is a built-in Exception class in PHP. by default, the default built-in Exception class in PHP is used. However, we can customize it, custom exception classes that meet your needs.
When a custom exception class is used, throw new myException also needs to throw a custom exception class name.
You also need to capture custom exception classes during catch.
If an exception is thrown and not captured, an error occurs in the PHP code, as shown below:
[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 details:
[Php]
Class exception
{
Protected $ message = 'unknow exception'; // custom exception information
Protected $ code = 0; // defined exception code
Protected $ file; // name of the PHP program with an exception
Protected $ line; // The PHP line number with an exception
// Constructor used to pass user-defined 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 for passing an exception in array format
Final function getTraceAsString (); // returns the getTrace function information formatted as a string.
Function _ toString (); // reload-able, used to return output strings
}
The default PHP exception class contains many custom variables and methods.
$ E-> getMessage generally gets error information
$ E-> getCode () is generally used to obtain error codes.
$ E-> getFile () get the wrong file information
$ E-> getLine () get the wrong number of rows
$ E-> getTrace () file path after an exception
_ ToString () can be reloaded to return output strings.
PHP exception class:
Capture exceptions. you can throw an exception when MYSQL cannot be connected or the service logic is incorrect and receive the exception at the top of the code.
Exception analysis and processing. When an exception occurs, you can capture the error information and write the log.
You can return friendly prompts. for example, the WEB side can return the form of a page, or the AJAX side can return the JSON data format.
Principle: if an exception is thrown, it must be captured; otherwise, an error will be reported in PHP.
 

Author: initphp

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.