Understanding Exceptions
Learn what exceptions are and how Python raises them.
Understanding Exceptions is a free Python Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction
What is an Exception?
# 1 / 0 # raises ZeroDivisionError
print('exceptions demo')Exception Hierarchy
print(issubclass(ValueError, Exception))
print(issubclass(Exception, BaseException))Common Built-in Exceptions
# Examples:
# int('abc') -> ValueError
# d['missing'] -> KeyError
# [1,2][9] -> IndexError
print('common exceptions')The Traceback
def f(): return 1/0
# f() # shows traceback
print('traceback demo')Exception Object
try:
int('abc')
except ValueError as e:
print(type(e).__name__, e)Catching Multiple Types
def parse(x):
try:
return int(x)
except (TypeError, ValueError):
return None
print(parse('abc'), parse(None))The Exception Group (3.11+)
# Python 3.11+:
# try: ...
# except* ValueError as eg:
# print(eg.exceptions)
print('exception group demo')Warnings vs Exceptions
import warnings
warnings.warn('This is deprecated', DeprecationWarning)
print('warning issued')sys.exc_info()
import sys
try:
1/0
except:
exc_type, exc_val, _ = sys.exc_info()
print(exc_type, exc_val)Exception Chaining
try:
int('abc')
except ValueError as e:
raise RuntimeError('parse failed') from eQuick Check
Recap
Keep Going
Frequently asked questions
Is the “Understanding Exceptions” lesson free?
Yes — the full text of “Understanding Exceptions” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Understanding Exceptions”?
Learn what exceptions are and how Python raises them. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Understanding Exceptions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Exceptions
- try / except / else / finally
- Raising and Re-raising Exceptions
- Custom Exception Classes