Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Handling specific exceptions
📖 Scenario: Imagine you are writing a small program that asks a user to enter two numbers and divides the first number by the second. Sometimes users might enter wrong data or try to divide by zero, which causes errors. We want to handle these errors carefully so the program does not crash and gives friendly messages.
🎯 Goal: You will build a program that safely divides two numbers by handling specific errors: ValueError when the input is not a number, and ZeroDivisionError when dividing by zero.
📋 What You'll Learn
Create two variables to store user input as numbers
Add a try-except block to catch ValueError and ZeroDivisionError
Print a friendly message for each specific error
Print the division result if no error occurs
💡 Why This Matters
🌍 Real World
Handling specific exceptions helps programs run smoothly even when users make mistakes, like entering wrong data or dividing by zero.
💼 Career
Knowing how to catch and handle errors is essential for writing reliable software that does not crash unexpectedly.
Progress0 / 4 steps
1
DATA SETUP: Create variables for user input
Create two variables called num1 and num2 by converting the strings '10' and '0' to integers using int().
Python
Hint
Use int('10') to convert the string '10' to the number 10.
2
CONFIGURATION: Prepare for error handling
Create a variable called result and set it to None. This will hold the division result later.
Python
Hint
Set result = None before trying the division.
3
CORE LOGIC: Use try-except to handle specific exceptions
Write a try block where you divide num1 by num2 and assign it to result. Add two except blocks: one for ZeroDivisionError that prints 'Cannot divide by zero.' and one for ValueError that prints 'Invalid number input.'.
Python
Hint
Put the division inside try: and catch errors with except ZeroDivisionError: and except ValueError:.
4
OUTPUT: Print the division result if no error
After the try-except blocks, write an if statement that checks if result is not None. If so, print f'Result: {result}' to show the division result.
Python
Hint
Use if result is not None: to check before printing.
Practice
(1/5)
1. What is the main purpose of using try-except blocks in Python?
easy
A. To catch and handle specific errors so the program doesn't crash
B. To speed up the program execution
C. To write comments inside the code
D. To create new functions automatically
Solution
Step 1: Understand the role of try-except
The try-except block is used to catch errors that happen during program execution.
Step 2: Identify the benefit of catching errors
By catching errors, the program can handle them gracefully and continue running instead of crashing.
Final Answer:
To catch and handle specific errors so the program doesn't crash -> Option A
Quick Check:
try-except = catch errors [OK]
Hint: Try-except blocks catch errors to avoid crashes [OK]
Common Mistakes:
Thinking try-except speeds up code
Confusing try-except with comments
Believing try-except creates functions
2. Which of the following is the correct syntax to catch a ZeroDivisionError in Python?
easy
A. try:
x = 1/0
except:
print('Error')
B. try:
x = 1/0
catch ZeroDivisionError:
print('Cannot divide by zero')
C. try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero')
D. try:
x = 1/0
except ZeroDivision:
print('Cannot divide by zero')
Solution
Step 1: Check the correct keyword for catching exceptions
Python uses except to catch exceptions, not catch.
Step 2: Verify the exception name spelling
The correct exception name is ZeroDivisionError, not ZeroDivision.
Final Answer:
try:
x = 1/0
except ZeroDivisionError:
print('Cannot divide by zero') -> Option C
Quick Check:
Use except + exact exception name [OK]
Hint: Use except with exact exception name to catch errors [OK]