In this section, we're going to see how to gracefully deal with panics. Unlike with errors, if you don't recover from panics, it will stop the execution of your program. Therefore, dealing with them is important if you want your program to continue. First of all, let's see how we can throw a panic in our Go program. You can simply use a keyword called panic
, which is a built-in function, type panicked, and run it to obtain the output:
There's another way to do this. Let's use another function here and write something. Let's imagine we're doing something and for some reason it just panicked. This might be a third-party method, which means it is located in a third-party package, so we may not have full control over that package. So, here, if you run the preceding code, this is what we're going to see in the application window, along with the message we want to write to the console, which is as follows:
We also see the stack trace of our panic
here. First, it...