Exception base class in PHP kernel

Source: Internet
Author: User
Tags define exception
In & lt; think about PHP language 3: Exception Handling & gt; there is a definition of exception: exception handling means that the program can respond to certain runtime errors and exceptions detected by other programs according to a standard method in the language. The time when an exception occurs cannot be determined. If a language does not include an exception handling mechanism, this will bring additional complexity to the language. There are three solutions for exception handling, while PHP5 uses

In < <思考php语言三:异常处理> > Definition of exceptions: exception handling refers to the ability of a program in a language to respond to certain runtime errors and exceptions detected by other programs according to a standard method. The time when an exception occurs cannot be determined. If a language does not include an exception handling mechanism, this will bring additional complexity to the language. There are three solutions for exception handling, and PHP5 uses a dedicated subroutine or class to separate exception handling.

Exception is the base class of all exceptions in PHP. since PHP5.1.0 was introduced, we can handle errors in an object-oriented way. The declaration of the Exception class is as follows:

Exception {/* attribute */protected string $ message; protected int $ code; protected string $ file; protected int $ 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 )}

Message indicates the content of the exception message, code indicates the exception code, file indicates the file name that throws the exception, and line indicates the row number in the file that throws the exception. The following describes these attributes and their corresponding methods from the perspective of the PHP kernel.

Message indicates the content of the exception message, which corresponds to the getMessage method. Message is a custom exception message, which is a null string by default. For the PHP kernel, when an Exception object is created, whether or not the message parameter affects the return value of the getMessage method, and whether the Exception contains the with message % s. Message member variables are used to better define exception classes.

Code indicates the exception code, which corresponds to the getCode method. Like the meesage member variable, code is user-defined. the default value is 0.

File indicates the file name for which an exception is thrown. it corresponds to the getFile method. the returned value is the file name of the execution file. in the PHP kernel, the field storing this file name is EG (active_op_array) -> when the value of filename is used to generate an opcode list, the PHP kernel assigns the file name of the previously compiled file to the filename attribute of opcode, for example, generating the op_array of a function, during op_array initialization, the assignment operation described above will be executed. the assignment here is passed through the compiled global variables. When the code is executed, EG (active_op_array) indicates the list of opcodes being executed.

Line indicates the row number in the file that throws an exception. it corresponds to the getLine method and returns an integer, that is, EG (opline_ptr)-> lineno. For the opcode generated by each PHP script, an initialization operation is performed during compilation. in This initialization operation, the PHP kernel assigns the currently compiled row number to the lineno attribute of the opcode. EG (opline_ptr) is the current opcode executed by the PHP Kernel. When an exception is thrown, the corresponding row number is the lineno attribute of the object.

In addition to the above four attributes, the exception class also includes a very important content: exception tracing information. In the exception class, you can obtain this information through the getTrace method. This method is equivalent to the built-in function debug_backtrace of PHP. At the code implementation level, they eventually call the zend_fetch_debug_backtrace function. In this function, code tracing information is returned by tracing the PHP call stack. There is also a method getTraceAsString corresponding to the getTrace method to return the stringized value, replacing the array with a string to return the exception tracing information.

In the constructor, add the $ previous parameter from PHP5.3.0 to indicate the previous exception in the exception chain. You can throw a new exception in the catch block and reference the original exception to provide more information for debugging.

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.