search
HomeCommon ProblemWhat to do if the API interface is abnormal

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.