Exception Handling in Python

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.

execption_handling1

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”.

execption_handling2.PNG

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”.

execption_handling3.PNG

Cheers!