Home >Backend Development >PHP Problem >How to design exception classes in PHP projects
Exception handling is classified as error handling. PHP has added the Exception exception handling class starting from 5.1.0.
1. Exception handling
PHP exception handling is similar to Java, using try, throw, and catch statements to code when an exception occurs. If the exception is not caught and the set_exception_handler() function is used for corresponding processing, a serious error (fatal error) will occur and an "Uncaught Exception" error message will be output.
1. try:
is used for code blocks where exceptions may occur.
2. throw:
Specifies how to trigger (trigger) exceptions, used to throw exceptions. Each throw must correspond to at least one catch.
3. catch:
Catch exceptions and create objects containing exception information.
Explanation: Let’s assume that php exceptions must be thrown to catch them.
Basic structure:
try{ #some codes throw new Exception("message"[,code[,...]]); } catch(Exception $ex){ #some codes }
2. PHP exception base class Exception
## Class summary:
Exception { /* 属性 */ protected string $message ; protected int $code ; protected string $file ; protected int $line ; /* 方法 */ 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 ) }
Example:
<?php try { throw new Exception("Some error message", 30);//抛出异常,设置异常代号为30 } catch(Exception $e) { echo "Exception:file:".$e->getFile().",message:" . $e->getMessage().",code:".$e->getCode()."line:".$e->getLine(); } ?>
3. Custom exception class
Example:
class customException extends Exception { public function errorMessage() { //error message $errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile() .': <b>'.$this->getMessage().'</b> is not a valid E-Mail address'; return $errorMsg; } }
Throwing and catching the exception:
try{ throw new customException("这是自定义异常。"); } catch(customException $ex){ #some codes }
4. Multi-catch catching exception
When different exceptions may be thrown in a try statement, there can be multiple corresponding catch blocks to capture different types of exceptions. Some points to note the same as in java: 1. Put the big exception catch at the back. Because when an exception is thrown, which catch is satisfied first is determined in order, and only one catch is executed at a time. 2. When a try is executed, at most one catch will be executed (when an exception occurs). That is, if a previous catch satisfies execution, subsequent catches will not be considered. Recommended tutorial:The above is the detailed content of How to design exception classes in PHP projects. For more information, please follow other related articles on the PHP Chinese website!