Some text some message..
Back Exception Concepts : 1️⃣ BaseException 2️⃣ sys module 3️⃣ sys.exc_info() 13 Oct, 2025

We’ll explore:

1️⃣ BaseException
2️⃣ sys module
3️⃣ sys.exc_info() — the hidden hero behind exception handling


🌟 1️⃣ BaseException — The Root of All Errors in Python


🧩 What it is:

BaseException is the superclass (parent) of all built-in exceptions in Python.

Think of it like a big family tree of all possible errors.
All other errors (like ValueError, ZeroDivisionError, etc.) are its children 👇


🌳 The Exception Family Tree

BaseException
│
├── SystemExit
├── KeyboardInterrupt
└── Exception
    ├── ArithmeticError
    │   ├── ZeroDivisionError
    │   └── OverflowError
    ├── ValueError
    ├── TypeError
    ├── FileNotFoundError
    └── ...

💬 Example:

try:
    1 / 0
except BaseException as e:
    print("Caught by BaseException:", e)

🟢 Output:

Caught by BaseException: division by zero

BaseException can catch any kind of error — even system-level ones like keyboard interrupts.

💡 But in most cases, we use Exception (a subclass of BaseException)
to avoid catching things like system exits or interrupts unintentionally.


🎯 So remember:

Term Description
BaseException The top-most parent of all exceptions
Exception The parent of most “normal” runtime errors
Other Errors All the “children” (like ValueError, TypeError, etc.)

⚙️ 2️⃣ The sys Module — System Utilities for Python


🧠 What is sys?

sys = System module — gives you access to Python’s runtime environment.

It lets you:

  • interact with the Python interpreter

  • get information about current exceptions

  • exit the program

  • access command-line arguments

  • manipulate Python paths, etc.


💬 Example:

import sys

print(sys.version)      # Shows Python version
print(sys.platform)     # Shows current OS
print(sys.path)         # Shows where Python looks for modules

🟢 Output:

3.10.12 (tags/v3.10.12)
win32
['C:\\Users\\abhia\\anaconda3\\lib', ...]

⚡ Why it matters in exception handling

The sys module includes a built-in function:

sys.exc_info()

This is what your code snippet uses!


🧩 3️⃣ sys.exc_info() — The Detective 🕵️‍♂️ of Errors


📜 Definition:

sys.exc_info() returns detailed information about the most recent exception that was caught.

When you call it inside an except block,
it gives a tuple of 3 things:

Value Meaning Example
exc_type The type/class of exception <class 'ZeroDivisionError'>
exc_value The actual error object ZeroDivisionError('division by zero')
exc_tb The traceback object (where it happened) <traceback object at 0x...>

💬 Example:

import sys

try:
    x = 1 / 0
except Exception as e:
    exc_type, exc_value, exc_tb = sys.exc_info()
    print("Type:", exc_type)
    print("Value:", exc_value)
    print("Traceback:", exc_tb)

🟢 Output:

Type: <class 'ZeroDivisionError'>
Value: division by zero
Traceback: <traceback object at 0x0000021A3...>

🧠 What is a traceback?

A traceback is the path the program took before it crashed
it tells you exactly where in the code the error occurred.

You can use the traceback module to pretty-print it:

import traceback
traceback.print_tb(exc_tb)

🎯 Why it’s used in custom error classes

When you build your own error-handling class:

exc_type, exc_value, exc_tb = sys.exc_info()

This helps you store or log:

  • What kind of error happened

  • What message it had

  • Where in the code it happened

So later you can format or display a clean error message for debugging or logs.


🌈 Summary (Color-coded Cheatsheet)

🔹 Concept 🔸 Meaning 💡 Example
🧱 BaseException Parent of all errors except BaseException:
⚙️ sys module Python system info sys.version, sys.exit()
🕵️ sys.exc_info() Gives type, value, and traceback of latest error exc_type, exc_value, exc_tb = sys.exc_info()
🪄 traceback Helps show where the error happened traceback.print_tb(exc_tb)

🌟 Visualization:

┌──────────────────────────────────────────────────────────────┐
│ try:                                                        │
│     1 / 0                                                   │
│ except Exception as e:                                      │
│     exc_type, exc_value, exc_tb = sys.exc_info()             │
│                                                              │
│     exc_type   →  <class 'ZeroDivisionError'>               │
│     exc_value  →  'division by zero'                        │
│     exc_tb     →  <traceback object>                        │
└──────────────────────────────────────────────────────────────┘