Home  >  Article  >  What to do if the API interface is abnormal

What to do if the API interface is abnormal

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-08-21 09:45:0521831browse

What to do if the API interface is abnormal

Exception:

Abnormal situations that occur during program development are exceptions. For example, the divisor is 0, the parameter is null, the member variable or method of the parameter is called, and the array subscript is out of bounds.

Exceptions are divided into two major types:

(1)Exception: Programmers can solve: null pointer, divisor is 0, array subscript out of bounds.

(2) Error: something that programmers cannot solve: such as memory overflow.

Throwable is the parent class of these two types

Exception classification:

Classification according to whether the compiler checks:

非检查性异常:也称之为运行时异常,即编译器在检查语法期间,不做异常检查。
检查性异常:也称之为编译时异常,此时,编译器会强制检查语法中的异常情况。如有异常,在编译期需要做异常处理。

Exception structure:

RuntimeException:
         NullPointerException
         ArrayIndexOutOfBoundsException
         ClassCastException
         IllegalArgumentExcetion
         NumberFormatException
IOException:
         EOFException
         FileNotFoundException

Exception handling:

(1) When an exception occurs, terminate the program.

(2) When an exception occurs, we use a processing mechanism to handle the exception. No need to terminate the program.

体验度:尽可能的选择异常处理机制。

Exception handling mechanism:

Basic idea:

In the code block area where exceptions may occur, try to check. If exception information occurs, we encapsulate the information into an object of a certain exception type, and then capture and handle it.

Related recommendations: "FAQ"

 try{
     可能出现异常的代码块
   /*如果有异常,jvm会将异常信息,封装成对象
      将地址信息赋值给catch中的形参
   */
 }catch(Exception e){
          进行处理e
 }

Multiple catch statement blocks:

When we try to capture exception objects, we want to When handling different exception objects separately, multiple catch statement blocks need to be used.

Note: When an exception message occurs in try, the subsequent part of the code block in try will not be executed. Enter the corresponding catch code block to perform processing.

In the case of multiple catch statement blocks, the order of writing the exception types handled by catch:

Write the subclass exception type first, and then write the parent class exception.

throw: Throw keyword, exceptions that occur in this method will not be processed by try-catch.

Instead, it is thrown to the caller for processing. The throw keyword needs to be used.

throws: Declares exception keywords, usually used in the definition of methods to notify the caller.

(1)当throw的异常对象为检查性异常,方法上必须throws此异常类型。
(2)如果throw的异常对象为非检查性异常,方法上不必throws此异常类型。

Usage of throws when rewriting method

Possible:

(1)可以相同。
(2)可以是部分。
(3)异常的子类型可行,也可以是多个子类型。

Not feasible:

(1)不同的异常类型,也不可以多声明不同类型的异常类型。
(2)异常的父类型不行。

finally:

provides a unified exit for try-catch. Regardless of whether an exception occurs in the try and catch statement blocks, the code block in finally will be executed eventually.

Usually used to handle operations such as closing some resources:

For example: closing operations when reading files, or deleting temporary files

The finally statement block is optional.

When there is a return in the statement, run it sequentially first. If it encounters a return, suspend its return value first, then run the code block in finally and then execute return (when there is no return in finally, the return will not be processed. value has an impact)

The above is the detailed content of What to do if the API interface is abnormal. 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
Previous article:arm64 differenceNext article:arm64 difference