Exception Handling in Python

Exception Handling in Python

Mastering Exception Handling in Python

ยท

3 min read

In this article, you will learn about exception handling in python.

What is an exception?

Let's start understanding Exception in python by executing the below code snippets. You can execute the code on your machine or any online compiler like this.

print(x)
print("Hello World")

You will get an error like the below image.

It says that the name 'x' is not defined which is obvious because in our example, we are trying to print the value of x without defining it and hence we got an error also program execution stopped once we got an error, therefore, we do not see Hello World as output.

In python whenever an error occurs or we get an exception, it will stop execution and show an error message. So Exception can be defined as an event that occurs during the execution of a program that disrupts the normal flow of instructions.

Now let's learn about how to handle these exceptions and continue the normal execution of python scripts.

Handle exceptions using try-catch

Let's execute below code snippets.

try:
  print(x)
except:
  print("An exception occurred")
print("Hello World")

This time you will see An exception occurred & Hello World as output like the below image.

So what we did here is we moved the code which was giving exceptions inside the try and also added an except statement to print any customized message if the code inside the try gives an exception.

In short, we can use a try block to handle the exception & except to write the code which will be executed if any exception occurs.

Now, let's execute some more code to handle specific exceptions.

try:
  print(2/0)
except ZeroDivisionError:
  print("Division by zero is not allowed")
except:
  print("Something else gone wrong")

In the above code snippets, we tried to divide 2 by zero, and as we know it is not possible to divide any number by zero so an exception will occur and it will get handled by line number 3 and Division by zero is not allowed will be shown as output.

What's else

Lets's execute one more code snippet.

try:
  print(2/4)
except ZeroDivisionError:
  print("Something went wrong")
else:
  print("Nothing went wrong")

There is nothing wrong with the code under the try block hence 0.5 will be printed and except block will be skipped and the code under the else block will also be shown in the output.

In short, the else keyword is used to define a block of code that will be executed if no exception is raised.

Finally

Let's execute one more piece of code and it will be the last code that you will execute.

try:
  print(x)
except:
  print("Something went wrong")
finally:
  print("After try-except")

You will see Something went wrong & After try-except as output.

Code under finally block will be executed irrespective of whether an exception occurs or not.

What will be the output?

Can you tell me what will be the output of below code snippet?

try:
  print(x)
except ZeroDivisionError:
  print("Something went wrong")
else:
  print("No Exception occured")
finally:
  print("After try-except")

Also, check out the below video on Mastering Exception Handling in Python.

Conclusion

So whenever an error occurs while executing the python script execution will stop and an error message will be generated.

In the try block, we write the code which can raise an error.

Except block lets us handle the raised exception.

Code under else will be executed if no exception is raised.

Finally*, the block will always execute.*

I hope I was able to explain exception handling in python.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here. Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

ย