---
title: "How do I make an exception when catching errors in Python?"  
description: "How do I make an exception when catching errors in Python?"  
author: "Revati S Misra"  
published: 2023-07-20  
updated: 2023-07-20  
canonical: https://www.mindstick.com/forum/159171/how-do-i-make-an-exception-when-catching-errors-in-python  
category: "python"  
tags: ["exception handling", "exception"]  
reading_time: 2 minutes  

---

# How do I make an exception when catching errors in Python?

How do I make an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) when [catching](https://www.mindstick.com/forum/1582/catching-messagebox-result) [errors](https://answers.mindstick.com/qa/116170/fresh-fir-against-gandhis-in-national-herald-case-cover-up-for-ed-s-own-errors) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?

## Replies

### Reply by Aryan Kumar

Sure, here is how you can make an exception when catching errors in Python:

Python

```plaintext
def divide_numbers(x, y):
  try:
    result = x / y
  except ZeroDivisionError as e:
    # Create an exception
    raise Exception("Division by zero")

result = divide_numbers(10, 0)
```

In this example, the `divide_numbers()` function takes two numbers as input and divides them. If the denominator is zero, the `ZeroDivisionError` exception is raised. The `try`/`except` block catches the exception and creates a new exception called `Exception`. The new exception is raised and will be handled by the next `try`/`except` block.

Here is a breakdown of how the `try`/`except` block works:

- The `try` keyword indicates the beginning of a block of code that may throw an exception.
- The `except` keyword is followed by the name of the exception that you want to catch. In this case, we are catching the `ZeroDivisionError` exception.
- The code in the `except` block will be executed if the exception is raised. In this case, the code creates a new `Exception` exception.
- The `raise` keyword is used to raise an exception. In this case, we are raising the new `Exception` exception that we created.


---

Original Source: https://www.mindstick.com/forum/159171/how-do-i-make-an-exception-when-catching-errors-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
