How to design exception classes in PHP projects

王林
Release: 2023-02-25 15:12:01
Original
2132 people have browsed it

How to design exception classes in PHP projects

PHP exception classes and exception handling operations

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
}
Copy after login

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 )
}
Copy after login

Description:

It can be seen from this base class that the php exception object mainly contains abnormal text information (message) and exception code /Codename (code, should be used for developer identification), the file where the exception occurred (file, the php file where the exception occurred), and the specific location where the exception occurred (line, the line number where the exception was thrown).

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();
}
?>
Copy after login

3. Custom exception class

Example:

class customException extends Exception
 {
 public function errorMessage()
 {
 //error message
 $errorMsg = &#39;Error on line &#39;.$this->getLine().&#39; in &#39;.$this->getFile()
 .&#39;: <b>&#39;.$this->getMessage().&#39;</b> is not a valid E-Mail address&#39;;
 return $errorMsg;
 }
 }
Copy after login

Throwing and catching the exception:

try{
throw new customException("这是自定义异常。");
}
catch(customException $ex){
#some codes
}
Copy after login

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:

PHP video 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template