Python Exceptional Handling

Posted by

Python exceptional handling will provide a smooth and crash free user experience by properly handling the unexpected crashes in your app.

An exception is an event that occurs during the execution of a program that disrupts the normal flow of the program’s instructions. When an exception occurs, the program terminates the current operation and jumps to a pre-defined exception handling routine.

In software apps crashing is the most common scenario faced by almost every app. No app is build with a 100% crash free usage.

It’s not like inefficiency in coding but the device, network and other parameters also affects the functioning of the app.

We need to handle these crashes efficiently using a standard approach provided by python, because some times it might be abnormal to stop a user flow and losing data due to app closure.

Some common types of exceptions include:

ArithmeticException :

We find this mostly in calculations, As this exception is thrown when an arithmetic operation produces an error, such as dividing by zero.

NullPointerException :

When any value is missing or null received this exception is thrown such as when a program attempts to use a null object reference.

IOException :

When the file is not accessible or database this exception is thrown stating that an input or output operation failed.

IllegalArgumentException :

When the argument passed is invalid or doesn’t match the type this exception is thrown.

RuntimeException:

This is a most common general-purpose exception class that can be used to represent a wide range of runtime errors, such as memory insufficient, index out of bounds error.

Python Exceptional Handling Video Tutorial :

Watch the video tutorial on exception handling in python.

Let us see an example

a = 10
b = 5

print(a/b)

output :

2

Now let us try to see the same example where we get an error

a = 10
b = 0

print(a/b)

output:

Can you guess this output ?? We will be getting a ZeroDivisionError : divide by zero.

How to handle these errors ?

Errors make a big flaw in app functionality i..e, it stops user flow and ends the app some times it might lose app state and data user entered too.

You can make use of Try Except in handling your errors.

In Try block you will be trying the code which is vulnerable to crash

In Except block you will handle it such that crash won’t happen and also display a message stating the issue happened.

a = 10
b = 0

try :
    print(a/b)

except Exception:
    print("You have got an error")

Output:

Now you will not get any error and a message is displayed instead.

Also you can make use of finally block i have explained its usage clearly in video tutorial.

Therefore a proper handling of exceptions is an important part of writing robust and reliable software. By anticipating and handling exceptions, developers can create programs that are more resilient and less prone to errors.

If you have any query’s in this tutorial on Python Exceptional Handling do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates..

Go through the video tutorial for detailed implementation.Watch the complete video course on python.

python exceptional handling