Home  >  Article  >  Backend Development  >  What is php exception handling method

What is php exception handling method

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-27 16:16:273490browse

What is php exception handling method

1. Exception handling

Accidents are unexpected things that happen during the running of the program. Use exceptions to alter the normal flow of a script.

Related recommendations: "PHP Getting Started Tutorial"

if(){
   }else{
   }
try {
    }catch(异常对象){
   }

1. If there is no problem with the code in try, execute the code in try after the catch.

2. If an exception occurs in the code in try, an exception object (using throw) will be thrown to the parameters in catch. Then the code in try will not continue to execute and will be executed directly. Jump to the catch to execute, the execution in the catch is completed, and then continue to execute.

Note: Prompt what exception occurred. This is not the main thing we need to do. We need to solve the exception in catch. If it cannot be solved, go out to the user.

2. Define an exception class by yourself

Function: It is to write one or more methods to solve the problem of handling this exception when it occurs.

1. Define your own exception class, which must be a subclass of Exception (built-in class).

2. Only the constructor and toString() in the Exception class can be rewritten, and the others are final. .

The above is the detailed content of What is php exception handling method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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