Remember
An exception interrupts normal execution when an operation cannot finish. Catch an expected exception to choose a recovery path.
An exception interrupts normal execution when an operation cannot finish. Catch an expected exception to choose a recovery path.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = '12'
try:
number = int(text)
print(number)
except ValueError:
print("Please enter a whole number")Top-level scope
'12'Object 2 · int12An exception interrupts normal execution when an operation cannot finish. Catch an expected exception to choose a recovery path.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = '12'
try:
number = int(text)
print(number)
except ValueError:
print("Please enter a whole number")
|
v
DISPLAYED RESULT
12
FINAL BINDINGS
text --> object 1 (str)
number --> object 2 (int)
object 1: '12'
object 2: 12Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = '12'
try:
number = int(text)
print(number)
except ValueError:
print("Please enter a whole number")Top-level scope
'12'Object 2 · int12The example has finished. Compare the final output with the values and references above.
12
What do you expect this program to print?
text = '12'
try:
number = int(text)
print(number)
except ValueError:
print("Please enter a whole number")In Python, an exception is an error that disrupts the normal flow of a program. When something goes wrong during execution—like dividing by zero or trying to access an index that doesn’t exist—Python stops and raises an exception.
Knowing how and why they occur is the first step in building resilient, bug-free Python applications. Later, you'll learn how to catch and handle them, but first, we must learn to recognize them.
Python raises an exception during runtime when it encounters an issue that it doesn’t know how to handle. This stops the execution of the program and displays an error message (also called a traceback).
x = 5
y = 0
result = x / y
print(result)
Traceback (most recent call last):
File "main.py", line 3, in <module>
result = x / y
ZeroDivisionError: division by zero
Python gives us a traceback—a report of where the error occurred. In this case, it tells us exactly which line failed and what kind of error it was: ZeroDivisionError.
fruits = ["apple", "banana", "cherry"]
print(fruits[5])
Traceback (most recent call last):
File "main.py", line 2, in <module>
print(fruits[5])
IndexError: list index out of range
Here, the list only has 3 elements, indexed 0 through 2. Trying to access index 5 raises an IndexError.
| Exception | Description |
|---|---|
ZeroDivisionError | Raised when dividing by zero |
TypeError | Raised when an operation is applied to an object of inappropriate type |
NameError | Raised when a variable is not defined |
IndexError | Raised when a sequence index is out of range |
KeyError | Raised when a dictionary key isn’t found |
ValueError | Raised when a function receives an argument of right type but inappropriate value |
AttributeError | Raised when an invalid attribute reference is made |
ImportError | Raised when a module or name cannot be found during import |
Python exceptions are runtime errors that occur when something goes wrong while executing your code. In the next section, we’ll explore how to catch and handle these exceptions using try and except.
Connect what you noticed in the example with the idea below.
An exception interrupts normal execution when an operation cannot finish. Catch an expected exception to choose a recovery path.
An exception is just a normal return value.
This is the misconception to avoid.Reaching the recap and completing the quizzes are separate milestones.