Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 10

Robust Language

Exception handling: try/except, ZeroDivisionError, and ValueError.

Beginner10 min

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 ZeroDivisionError and ValueError?
  • 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.

User input to runtime problem to friendly message

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:

text
ZeroDivisionError: division by zero

But an end user should see:

text
Cannot divide by zero

Why?

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:

  1. Detecting runtime errors
  2. Preventing abrupt program failure
  3. Showing meaningful messages
  4. Continuing safely when appropriate

Basic structure

python
try:
    # Risky code
    pass
except SomeError:
    # Friendly handling
    pass

The keywords are:

  • try → code that may fail
  • except → what to do if it fails

Without Exception Handling

Consider this program.

python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

c = a / b

print("Division =", c)

User input

text
Enter first number: 10
Enter second number: 0

Result

text
ZeroDivisionError: division by zero

The program stops immediately.

With Exception Handling

Now wrap the risky code.

python
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

text
Enter first number: 10
Enter second number: 0

Output

text
Cannot divide by zero

The user receives a clear message instead of a technical traceback.

Understanding try and except

try block to exception decision to except block

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

python
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:

text
10
0

Output:

text
Cannot divide by zero

Scenario 2

Input:

text
Ten
5

Output:

text
Please enter numbers only

Different 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.

python
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.
  • try contains risky code.
  • except converts technical errors into friendly messages.
  • Common exceptions include ZeroDivisionError and ValueError.

Interview Questions

  1. Why is Python called a robust language?
  2. What is an exception?
  3. What is exception handling?
  4. What is the purpose of the try block?
  5. 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.