0% found this document useful (0 votes)
3 views18 pages

Understanding Python Standard Exceptions

Uploaded by

yuvasrisampathu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Understanding Python Standard Exceptions

Uploaded by

yuvasrisampathu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

PYTHON STANDARD

EXCEPTIONS
BY,
S. YUVASRI, [Link]
WHAT MAKES PYTHON’S EXCEPTION
MODEL UNIQUE
Python treats exceptions as objects, not just error codes.

Every exception is an instance of a class derived from base exception


.
You can inspect, subclass, and even modify exception behavior.

Python encourages "Easier to Ask for Forgiveness than Permission


" (EAFP) — meaning, just try and handle exceptions if they occur.
REAL-WORLD ANALOGY

Think of exceptions as fire alarms — they don’t stop the fire but alert
you to handle it properly.

If you ignore it, the whole “building” (program) can crash.

Exception handling provides controlled recovery instead of termination.


INTERNAL MECHANISM

When an error occurs:

I. Python creates an Exception object.


II. It searches the call stack for a try block that can handle it.
III. If none is found, the interpreter halts execution and prints a
traceback.

This search mechanism is called stack unwinding.


BASE EXCEPTION VS EXCEPTION
Base Exception → root of all exceptions (don’t subclass directly).

Exception → base for all typical errors in programs.

Other rare subclasses:


System Exit
Keyboard Interrupt
Generator Exit

These should rarely be caught; they indicate system-level events.


HIDDEN EXCEPTIONS YOU MAY NOT KNOW

Assertion Error — raised by the assert statement.

Not Implemented Error — placeholder for unfinished features.

Runtime Warning — not an error, but a subtle warning (use warnings module.

Stop Iteration — signals the end of an iterator.


EXCEPTION CHAINING

•Python allows linking exceptions using raise ... from ...

Example:
try:
int("abc")
except ValueError as e:
raise TypeError("Bad type") from e

•Output shows both exceptions → improves debugging clarity.


EXCEPTION HIERARCHY VISUALIZATION

Group exceptions conceptually:


• Logic-related: Assertion Error, Name Error, Attribute Error
• Arithmetic-related: Zero Division Error, Overflow Error
• Input/Output-related: OS Error, File Not Found Error
• Import-related: Import Error, Module Not Found Error
A visual tree makes this easy to digest.
LOOKUP AND DATA ACCESS ERRORS

Lookup errors often occur in data-heavy apps (AI/ML, databases).

Use .get() or .set default() to avoid Key Error.

Example:
my_dict = {"x": 10}
print(my_dict.get("y", "Default"))

Python provides safe lookup alternatives — a unique design for developer convenience.
EXCEPTION HANDLING IN MODERN
PYTHON
Python 3 introduced “Exception Groups” (Exception Group, Base Exception
Group).
These allow handling multiple concurrent exceptions — useful for async or
multiprocessing code.
Example:
raise ExceptionGroup("GroupError", [ValueError(), TypeError()])
A modern, powerful feature often missed in tutorials.
EXCEPTION BEST PRACTICES

Catch only what you can handle.

Avoid using except Exception unless necessary.

Use finally for cleanup, not logic.

Prefer logging over printing errors.

Document known exceptions in function docstrings.


COMMON MISTAKES TO AVOID

Catching Exception hides real problems.


Forgetting to close files or network sockets.
Using raise incorrectly — use raise without arguments inside except to re-raise
the current exception.
Example of bad handling:
try:
risky()
except:
pass # silently hides errors!
QUESTIONS

[Link] of the following is the base class for all exceptions in Python?

A) Error
B) Exception
C) Base Exception
D) Runtime Error
[Link] exception class should you rarely catch because it handles system-
level events?

A) Exception
B) Base Exception
C) System Exit
D) Keyboard Interrupt
TRUE OR FALSE

[Link] exceptions in Python inherit from the Exception class.

A)True
B)False
SHORT ANSWER

4. What’s the purpose of the raise keyword?

[Link] does raise ... from ... do?

6. What’s the use of the else block in try-except?


REFERENCES
Official Python Documentation — Built-in Exceptions (Python 3.13)
[Link] Python documentation

Official Python Tutorial — Errors and Exceptions


[Link] Python documentation

Real Python — Python’s Built-in Exceptions: A Walkthrough With Examples


[Link] Real Python

W3Schools — Python Built-in Exceptions Reference


[Link]

You might also like