---
title: "Checking condition while Exception handling in python"  
description: "Checking condition while Exception handling in python"  
author: "Revati S Misra"  
published: 2023-07-20  
updated: 2023-07-20  
canonical: https://www.mindstick.com/forum/159180/checking-condition-while-exception-handling-in-python  
category: "python"  
tags: ["exception handling", "exception", "python"]  
reading_time: 2 minutes  

---

# Checking condition while Exception handling in python

Checking [condition](https://www.mindstick.com/forum/12711/select-query-with-and-condition) while [Exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) 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 check a condition while [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) [handling](https://www.mindstick.com/forum/34585/file-handling) in Python:

Python

```plaintext
def my_function():
  try:
    # This line might raise an exception
    1 / 0
  except ZeroDivisionError as e:
    # Check if the number is even
    if num % 2 == 0:
      print("The number is even.")
    else:
      print("The number is odd.")
```

In this example, the function `my_function()` tries to divide 1 by 0. If this division is successful, the `try` block will be executed. However, if the division fails, a `ZeroDivisionError` exception will be raised and the `except` block will be executed.

In the `except` block, we check if the number is even. If the number is even, we print the message "The number is even." Otherwise, we print the message "The number is odd."

Here is another example:

Python

```plaintext
def my_function():
  try:
    # This line might raise an exception
    num = int(input("Enter a number: "))
  except ValueError as e:
    print("Invalid input. Please enter a valid number.")
  else:
    # Check if the number is greater than 10
    if num > 10:
      print("The number is greater than 10.")
    else:
      print("The number is less than or equal to 10.")
```

In this example, the function `my_function()` asks the user to enter a number. The `try` block tries to convert the user input to an integer. If the conversion is successful, the `try` block will be executed. However, if the conversion fails, a `ValueError` exception will be raised and the `except` block will be executed.

In the `except` block, we print the message "Invalid input. Please enter a valid number." Otherwise, we check if the number is greater than 10. If the number is greater than 10, we print the message "The number is greater than 10." Otherwise, we print the message “The number is less than or equal to 10.”


---

Original Source: https://www.mindstick.com/forum/159180/checking-condition-while-exception-handling-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
