Lesson 10
Robust Language
Exception handling: try/except, ZeroDivisionError, and ValueError.
What you will learn
By the end of this lesson, you'll understand:
- Why is Python called a robust (strong) language?
- What is an exception?
- What does exception handling mean?
- How do you handle
ZeroDivisionErrorandValueError? - Why do robust programs provide user-friendly error messages?
What does Robust mean?
A robust language is one that can handle unexpected runtime problems without confusing the user.
The biggest reason Python is called robust is its Exception Handling system.
Instead of crashing suddenly, Python allows you to recover gracefully.
What is an Exception?
According to your source:
Every runtime error is called an Exception.
An exception happens while the program is running, not while writing the code.
Common runtime exceptions
| Exception | Cause |
|---|---|
ZeroDivisionError |
Dividing by zero |
ValueError |
Invalid numeric input |
| Others | Different runtime problems |
Technical vs User-Friendly Messages
A developer understands this:
ZeroDivisionError: division by zeroBut an end user should see:
Cannot divide by zeroWhy?
| Technical Error | Friendly Message |
|---|---|
| For developers | Clear explanation |
| Hard for users to understand | Better user experience |
This is the goal of exception handling.
What is Exception Handling?
Exception handling is the process of:
- Detecting runtime errors
- Preventing abrupt program failure
- Showing meaningful messages
- Continuing safely when appropriate
Basic structure
try:
# Risky code
pass
except SomeError:
# Friendly handling
passThe keywords are:
try→ code that may failexcept→ what to do if it fails
Without Exception Handling
Consider this program.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = a / b
print("Division =", c)User input
Enter first number: 10
Enter second number: 0Result
ZeroDivisionError: division by zeroThe program stops immediately.
With Exception Handling
Now wrap the risky code.
try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = a / b
print("Division =", c)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Please enter numbers only")Same input
Enter first number: 10
Enter second number: 0Output
Cannot divide by zeroThe user receives a clear message instead of a technical traceback.
Understanding try and except
The program doesn't blindly crash—it follows a controlled path.
Handling Multiple Exceptions
Your source demonstrates handling two different exceptions.
| Exception | Friendly Message |
|---|---|
ZeroDivisionError |
Cannot divide by zero |
ValueError |
Please enter numbers only |
Example
try:
a = int(input())
b = int(input())
print(a / b)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Please enter numbers only")Scenario 1
Input:
10
0Output:
Cannot divide by zeroScenario 2
Input:
Ten
5Output:
Please enter numbers onlyDifferent exceptions require different handling.
Why Robust Programs Matter
Imagine you're building:
- Banking software
- Hospital management
- E-commerce checkout
- Property management CRM
A crash is unacceptable.
A robust application should:
- Validate input
- Handle failures
- Inform the user clearly
- Continue safely when possible
This is exactly what exception handling provides.
Practice Exercise
Write a division program that handles both errors.
try:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Result =", a / b)
except ZeroDivisionError:
print("Cannot divide by zero")
except ValueError:
print("Please enter valid numbers")Test these inputs
| Input | Expected |
|---|---|
10 and 2 |
Result = 5.0 |
10 and 0 |
Cannot divide by zero |
abc and 5 |
Please enter valid numbers |
Key Points
- Python is robust because it supports exception handling.
- Every runtime error is called an exception.
trycontains risky code.exceptconverts technical errors into friendly messages.- Common exceptions include
ZeroDivisionErrorandValueError.
Interview Questions
- Why is Python called a robust language?
- What is an exception?
- What is exception handling?
- What is the purpose of the
tryblock? - Which exception occurs when dividing by zero?
Summary
| Concept | Remember |
|---|---|
| Robust | Strong, reliable language |
| Exception | Runtime error |
try |
Risky code |
except |
Error handling |
ZeroDivisionError |
Division by zero |
ValueError |
Invalid numeric input |
A robust Python program doesn't assume everything will work—it anticipates failure and provides clear, user-friendly responses. This is a fundamental practice in real-world software development.
Next
Next lesson: Third-Party Modules — how pip and PyPI extend Python with thousands of reusable libraries for web, AI, data science, and automation.