#27 Golang - Error Handling - Understanding Panic and Recover

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this video we will learn error handling and go focusing on panic and recover in languages like Java and C error handling is primarily managed through the tri catch mechanism when an error occurs within a tri block the catch block catches it allowing the program to handle the error gracefully this approach provides a clear structure for handling exceptions and ensures that your application can respond to errors without crashing go takes a different approach instead of exceptions go has panic and recover for handling runtime errors a panic stops the normal execution of a function and starts panicking up the call stack let us first learn how to raise a panic it can be simply done with the method Panic like this this method breaks breaks the flow of the program now if we want to recover from the Panic we can use recover method like this if there is something to recover we print the Panic here but there is an issue here VSS code says this code is unreachable the reason is there is no Tri block here that tells it is followed by the catch block so we need to specify the recovery code before the Panic happens let's move the recovery code to a function we need to defer this code so that it can be executed at the end of the function now go knows how to recover before the Panic happens let's run the program here it has recovered from the Panic what if the recover block is not there the program exits with a nonzero exit code this approach even works if one of the function calls Panic let's move the Panic call to a function the code still recovers from the Panic now let's explore another Concept in Go's error handling rep panicking rep panicking is when you catch a panic with recover perform some operations and then decide to panic again this can be useful for logging errors or cleaning up resources before letting the Panic propagate up the call stack let us understand rep panicking with this code with this piece of code we ask the user to enter a number then we read what the user enters on the command line into a variable input then this code calls the process input function at the beginning of the main function we have this recover code block let's see what's there in process input method this method takes an argument input which is a string here we use par in method to convert the string to the integer type if this conversion fails this means the wrong data was entered by the user so we throw the error using panic this recover block catches the panic and recovers we print the error here we do the cleanup in the end we propagate Panic up the call stack to the main function this propagated Panic is now caught by this recover block let's run the program enter a number hm let's enter a string here is the first recover this print is from the recover Block in the main function let's run it again this time we enter a number and everything went well no Panic while Java's Tri catch blocks manage exceptions explicitly goes panic and recover provide a more implicit way of handling errors in Java checked exceptions Force error handling promoting a proactive approach in contrast go encourages handling errors only when necessary focusing on Simplicity and reducing boilerplate Cur Cod when using panic and recover and go remember these best practices use Panic only for unrecoverable errors situations where the application cannot continue recover is best used in top level functions such as those handling web requests or go routines to prevent the entire application from crashing always clean up resources use defer for cleaning up resources whether a panic occurs or not we hope this tutorial helps you navigate Go's error handling with more confidence don't forget to like share and subscribe for more programming tutorials happy coding
Info
Channel: codeHeim
Views: 2,323
Rating: undefined out of 5
Keywords:
Id: YVkfdtV0fq8
Channel Id: undefined
Length: 6min 45sec (405 seconds)
Published: Tue Mar 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.