Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money
We wrote three cases together last time, let’s continue this time, this time will be slightly different from the last time
Case 1
Another very common example is to use defer to catch exceptions. That is, when the program crashes, the defer statement can help us by catching exceptions and executing the logic we want to get the program back on track
- In C/C++, we can make exceptions, such as dividing by 0, or an array out of bounds, etc., which will cause the program to crash
- Exceptions can also be created in GO in this way, but we can also use panic to crash programs
- Write a simple example to crash your own program
func testDefer(a) {
defer func(a) {
fmt.Println(Before the "panic")
}()
panic("i panic")
defer func(a) {
fmt.Println(After the "panic"()})}func main(a) {
testDefer()
fmt.Println("program over")}Copy the code
In fact, if we look at the Goland tool, we can see that the following sentence will not be executed
Defer func() {fmt.println (" after panic ")}() defer func() {fmt.println (" after panic ")}()
After actually running the program, the result is as follows:
Println(“program over”) is not printed and there is a specific panic message in the image
As you can see from the above, programs after Panic will not execute. The defer statement before Panic will execute because it was pushed first
So let’s catch an exception
Or the above code, we catch the exception, is to add a sentence can be
func testDefer(a) {
defer func(a) {
fmt.Println(Before the "panic")
if err := recover(a); err ! =nil {
fmt.Println("XDM, I caught an exception. The program doesn't crash.")}} ()panic("i panic") // Trigger defer off the stack
defer func(a) {
fmt.Println(After the "panic"()})}func main(a) {
testDefer()
fmt.Println("program over")}Copy the code
Look at the code above, the program after Panic will still not be executed, but we have added the recover() statement, which will help us catch exceptions and handle exceptions
After executing the above code, the effect is as follows:
> go run main.goBefore panic XDM, I caught an exception, the program does not need to crashCopy the code
According to the printing, we can see that the conclusion is consistent with the conclusion of the above example, and after the exception capture is added, the crash stack information of the program will not be printed, and the program exits normally. We can see that program over is printed normally
Case 2
Let’s play with the latter case again
There is always one panic above, so how do you play if there are multiple panics? Can defer still capture the panic information? If there are two panic occurrences, can defer also catch two?
func main(a) {
defer func(a) {
if err := recover(a); err ! =nil {
fmt.Println("XDM, I caught an exception.")}} ()defer func(a) {
panic("defer func panic")
}()
panic(" i panic")
panic(" last panic")
fmt.Println("baibai")}Copy the code
If you look at the code above, you know what to do, but if you look at Goland, you know that the statement after Panic will not be executed, right
Try to analyze it. As mentioned earlier, defer came in stack order
- The first derfer is pushed first
- The second defer was pushed back
- Executing panic should cause an error
i panic
- However, when the program exits, it will exit the stack and execute the second defer first, which is also a panic, so the panic information will be overwritten and an error will be reported
defer func panic
- Exit the stack, and at the first defer, the exception is caught and the program exits normally, so executing the program will have the following results
XDM, I've caught an exceptionCopy the code
Well, that’s enough for today, interested friends can also play
Welcome to like, follow and favorites
Friends, your support and encouragement, I insist on sharing, improve the quality of the power
All right, that’s it for this time
Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.
I am Nezha, welcome to like, see you next time ~