# Exception Handling in Python

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](https://www.programiz.com/python-programming/online-compiler/).

```python
print(x)
print("Hello World")
```

You will get an error like the below image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674903411947/a5ecdf03-47da-4b9d-988a-3b65fc49188a.png align="center")

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.

```python
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674904729694/e060d374-448d-43ce-8b40-5a5fe3b900d7.png align="center")

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.

```python
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674905827476/f0667c01-1b7e-42f1-ad91-efd74ae48d22.png align="center")

### **What's else**

Lets's execute one more code snippet.

```python
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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674906167977/b170cf2c-4a08-4a33-95f0-82486e6b2f59.png align="center")

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.

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674906600831/14b2b9f4-577b-44ca-b31d-4c693d494561.png align="center")

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?

```python
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.**

%[https://www.youtube.com/watch?v=Inp6ozuBdVQ] 

### 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**](https://twitter.com/ShubhamLashkan). Let me also know which topic I should write about next.
