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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP and ManticoreSearch Development Guide: Quickly Create a Search API Search is one of the indispensable features in modern web applications. Whether it is an e-commerce website, social media platform or news portal, it needs to provide an efficient and accurate search function to help users find the content they are interested in. As a full-text search engine with excellent performance, ManticoreSearch provides us with a powerful tool to create excellent search APIs. This article will show you how to

How to crawl and process data by calling API interface in PHP project? 1. Introduction In PHP projects, we often need to crawl data from other websites and process these data. Many websites provide API interfaces, and we can obtain data by calling these interfaces. This article will introduce how to use PHP to call the API interface to crawl and process data. 2. Obtain the URL and parameters of the API interface. Before starting, we need to obtain the URL of the target API interface and the required parameters.

Title: How to deal with Laravel API error problems, specific code examples are needed. When developing Laravel, API errors are often encountered. These errors may come from various reasons such as program code logic errors, database query problems, or external API request failures. How to handle these error reports is a key issue. This article will use specific code examples to demonstrate how to effectively handle Laravel API error reports. 1. Error handling in Laravel

In the world of data-driven applications and analytics, APIs (Application Programming Interfaces) play a vital role in retrieving data from various sources. When working with API data, you often need to store the data in a format that is easy to access and manipulate. One such format is CSV (Comma Separated Values), which allows tabular data to be organized and stored efficiently. This article will explore the process of saving API data to CSV format using the powerful programming language Python. By following the steps outlined in this guide, we will learn how to retrieve data from the API, extract relevant information, and store it in a CSV file for further analysis and processing. Let’s dive into the world of API data processing with Python and unlock the potential of the CSV format

ReactAPI Call Guide: How to interact with and transfer data to the backend API Overview: In modern web development, interacting with and transferring data to the backend API is a common need. React, as a popular front-end framework, provides some powerful tools and features to simplify this process. This article will introduce how to use React to call the backend API, including basic GET and POST requests, and provide specific code examples. Install the required dependencies: First, make sure Axi is installed in the project

Oracle is a world-renowned database management system provider, and its API (Application Programming Interface) is a powerful tool that helps developers easily interact and integrate with Oracle databases. In this article, we will delve into the Oracle API usage guide, show readers how to utilize data interface technology during the development process, and provide specific code examples. 1.Oracle

OracleAPI integration strategy analysis: To achieve seamless communication between systems, specific code examples are required. In today's digital era, internal enterprise systems need to communicate with each other and share data, and OracleAPI is one of the important tools to help achieve seamless communication between systems. This article will start with the basic concepts and principles of OracleAPI, explore API integration strategies, and finally give specific code examples to help readers better understand and apply OracleAPI. 1. Basic Oracle API

How to use MongoDB to develop a simple CRUD API In modern web application development, CRUD (Create, Delete, Modify, Check) operations are one of the most common and important functions. In this article, we will introduce how to develop a simple CRUD API using MongoDB database and provide specific code examples. MongoDB is an open source NoSQL database that stores data in the form of documents. Unlike traditional relational databases, MongoDB does not have a predefined schema