PHP Error Explanation

Source: Internet
Author: User
Tags getmessage php error stack trace
PHP Exception Explanation

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:

 
  GetMessage ();}
    1. throw new Exception (' XXX '): Throws an exception
    2. 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.
    3. Catch: A block of code catches an exception and creates an object that contains the exception information

Custom Exception classes:

Class MyException extends Exception {public Function errormessage () {  $errorMsg = ' Error in line '. $this->getline ( ). ' In '. $this->getfile ()  '. $this->getmessage (). ' was not a valid e-mail address ';  return $ERRORMSG;  }} try {  throw new myexception ($email);} catch (MyException $e) {echo $e->errormessage ();}
    1. The MyException class is a custom exception class that must inherit exception
    2. 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.
    3. When you use a custom exception class, throw new MyException throws an exception and also throws a custom exception class class name
    4. Catch is also required to capture custom exception classes
    5. If an exception is thrown without capturing processing, a PHP code error will occur, as follows:
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:

Class exception{    protected $message = ' Unknow exception ';//Custom exception information    protected $code = 0;//definition of the exception code    protected $file; Exception PHP Program name    protected $line;//The PHP line number of the exception/    /is used to pass the user custom exception information and the user custom exception code constructor 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 the Gettrace function information formatted as a String functions    __tostring ();//Can be overloaded To return a string that can be output}
    1. You can see that there are many custom variables and methods in the PHP default exception class
    2. $e->getmessage is generally getting error messages
    3. $e->getcode () usually gets the error code
    4. $e->getfile () Gets the wrong file information
    5. $e->getline () Gets the wrong number of rows
    6. File path $e->gettrace () exception
    7. __tostring () can generally be overloaded to return a string that can be output

The role of PHP exception classes:

    1. Catching exceptions can usually be thrown at the top of the code when the MySQL link is not available, or when the business logic is wrong.
    2. Exception analysis processing. You can catch an exception error message and write to the log at the time of the exception.
    3. You can return friendly hints, such as web-side return page form, or return JSON data format on the Ajax side.
    4. Principle: If an exception is thrown, it must be captured, or PHP will be error.


  • Related Article

    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.