An exception is a very known to every programmer! Afterall we most of the time seems to be playing with this keyword. Python try except and finally statements allow you to handle these exceptions.
Python exception or error doesn’t indicate that there is always something wrong with the logic or algorithm.
There are some circumstances where your program is absolutely correct, still you encounter run-time error that situation is known as an EXCEPTION.
At this point, our run time errors resulted in a termination of program execution. Rather terminating the program execution, program could detect the error before hand and handle it in a very smart way, that concept is called as Exceptional handling.
An Exceptional handling is a standard python mechanism allows programmers to deal with the run-time errors.
Examples of exceptional errors: integer division by zero, attempting to convert a non-number to an integer, accessing a list with an out-of-range index, and using an object reference set to None.
1. ZeroDivisionError: division by zero
You have a program where you are doing some calculations. You have defined two integer variables and assigned value to them. Now you are trying to attempt division operation.
Your code is absolutely perfect, and it will give you division answer but what will happen if user enters 0 value for variable N2! Always remember, you never have a control over an end user.
In this case program execution will be terminated with error – ZeroDivisionError: division by zero
2. ValueError: invalid
literal for int()
To continues with same above example where you expect user should enter integer number(digits) but consider user has entered some random characters or numbers in a word (say five instead of 5) for variable N1 then WHAT? It will throw this exception – ValueError: invalid literal for int()
Looking for Solution?
Yes, it is absolutely possible to write a code to avoid such kind of errors in python like other programming languages, but python has unique features which you can apply.
Python has built in conditional execution structure to handle the situation where you might encounter expected or unexpected run-time errors.
Python Try and except
try/except block to be added where you think potential exception can occur in your program.
Back to our previous example and we will see how it works. Now you know where possible exception error can come in our example so try to add try/except block into it.
Firstly, we will try display text message to the user so they can understand what went wrong! The technical error user might not understand so let’s change default error message with simple plain English text.
finally block always executes after try/except block execution. It does nothing extraordinary, just creates the space for you to execute something after try/except block.
So far, we have seen basic use of try/except, else and finally block in python. Mostly it is being used with the conditional loop statements and allow users to try one more time – with proper message on screen when they enter wrong values, for instance.
Write a program to demonstrate an
exceptional handling case with Loop statements!
In this example users can input any small integer value less than 100. They can enter number greater than 100 to complete the program execution. This code will not fail or terminate the execution if try block raise ValueError exception.