---
title: "How are exceptions handled in Python?"  
description: "How are exceptions handled in Python?"  
author: "ICSM Computer"  
published: 2025-09-14  
updated: 2025-09-15  
canonical: https://www.mindstick.com/forum/161910/how-are-exceptions-handled-in-python  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# How are exceptions handled in Python?

How are [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) handled in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)? [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with example.

## Replies

### Reply by Anubhav Sharma

### [Exception Handling](https://www.mindstick.com/forum/161263/how-do-you-handle-exceptions-in-python) Flow

- `try` **block**

   - Code that might raise an exception goes here.
   - If no exception happens, Python skips the `except` blocks.

- `except` **block(s)**

   - Handle specific exceptions.
   - You can have multiple `except` clauses to catch different error types.
   - The exception object (`as e`) contains details about what went wrong.

- `else` **block**

   - Runs **only if no exception was raised** in the `try` block.
   - Useful for code that should execute only when the `try` succeeded.

- `finally` **block**

   - Runs **always**, whether an exception occurred or not.
   - Commonly used for cleanup (closing files, releasing resources, etc.).

### Your Example

```python
try:
    x = 1 / 0
except ZeroDivisionError as e:
    print("Error:", e)        # runs because division by zero happens
else:
    print("No error")         # skipped because an exception occurred
finally:
    print("Always runs")      # always executes
```

## Output:

```plaintext
Error: division by zero
Always runs
```

### More Examples

**Multiple** `except` **clauses:**

```python
try:
    num = int("abc")
except ValueError:
    print("Invalid number")
except ZeroDivisionError:
    print("Division by zero")
```

## Catching all exceptions (not always recommended):

```python
try:
    risky_operation()
except Exception as e:
    print("Something went wrong:", e)
```

So in short:

- `try` → attempt risky code
- `except` → handle specific errors
- `else` → runs only if no error
- `finally` → always runs (cleanup)


---

Original Source: https://www.mindstick.com/forum/161910/how-are-exceptions-handled-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
