Home > Backend Development > Golang > How to intercept errors in golang

How to intercept errors in golang

silencement
Release: 2019-12-25 13:17:21
Original
1964 people have browsed it

How to intercept errors in golang

The error interface type is introduced in Golang as a standard mode for error handling. If the function returns an error, the return value type list must include error. The error processing process is similar to the error code in C language and can be returned layer by layer until it is processed.

Golang introduces two built-in functions panic and recover to trigger and terminate the exception handling process, and introduces the keyword defer to delay the execution of the function after defer.


The delayed function (the function after defer) will not be executed until the function containing the defer statement is executed, regardless of whether the function containing the defer statement passes the positive return

Ends normally, or ends abnormally due to panic. You can execute multiple defer statements in a function, and they are executed in the reverse order of declaration.


Recommended to study "

golang tutorial

"When the program is running, if you encounter situations such as referencing a null pointer, subscript out of bounds, or explicitly calling the panic function, etc. , then the execution of the panic function is first triggered, and then the delay function is called. The caller continues to pass panic, so the process keeps happening repeatedly in the call stack: functions stop executing, delayed execution functions are called, etc. If there is no call to the recover

function in the delay function, the starting point of the ctrip will be reached, the ctrip will end, and then all other ctrips, including the main ctrip (similar to the main thread in C language, will be terminated) Ctrip ID is

1).

Errors and exceptions are the difference between error and panic in terms of Golang mechanism. The same goes for many other languages, such as C/Java. There is no error but errno, and there is no panic

but there is throw.

Golang errors and exceptions can be converted to each other:

Errors are converted to exceptions. For example, the program logic attempts to request a certain URL. It can try for up to three times. If the request fails during the three attempts, it is an error. If it fails after the third try, the

failure will be upgraded to an exception. Exceptions turn into errors. For example, after the exception triggered by panic is recovered, the error type variable in the return value is assigned, so that the upper layer function

can continue the error handling process.

The above is the detailed content of How to intercept errors in golang. 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