Exception handling is a graceful way to manage execution of a program and display user friendly information when errors occur during execution of the programs.
Let’s look at the below example to get a better understanding. User needs to provide a number to the below program and reverse of the number will be returned.
If the user provides a numerical input the “try block” will be executed and the “finally block” will be executed.
However, if the user provides a non numeric input, an exception error “ValueError” will happen and code will execute the first “except” block and “finally block”.
Similarly if the user provides ‘zero’ as the input, an exception error “ZeroDivisionError” will happen and code will execute the second “except” block and “finally block”.
Cheers!
You must be logged in to post a comment.