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!

 

One thought on “Exception Handling in Python

  1. Pingback: Learn Python Step by Step | RP's Blog on data science

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s