: This article mainly introduces the php exception handling class. if you are interested in the PHP Tutorial, refer to it. PHP has many Exception handling classes, of which Exception is the base class for handling all exceptions.
Exception has several basic attributes and methods, including:
Message exception message content
Code exception code
File name that throws an exception
Line throws an exception on the number of lines in the file.
Common methods include:
GetTrace
GetTraceAsString string used to obtain exception tracking information
GetMessage
If necessary, you can inherit the Exception class to create a custom Exception handling class.
// The custom Exception class inherits the Exception base class Exceptionclass MyException extends Exception {function getInfo () {return 'custom error information';} of PHP ';}} 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. Throw new MyException ('error'); // specifies how to trigger an exception. Note: Each "throw" must correspond to at least one "catch". of course, multiple "catch"} catch (Exception $ e) {// "catch" code blocks can catch exceptions, create an object that contains the exception information echo $ e-> getInfo (); // Obtain the custom exception information echo $ e-> getMessage (); // Get the getMessage information inherited from the base class}
The above introduces the php exception handling class, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.