🟢 Beginner  ·  Lesson 18

Exception Handling

Exception Handling

What is Exception Handling?

Exception Handling means exception handling prevents a program from crashing when an error occurs during execution.

In real programs, this topic helps in handling wrong input. Learn the idea first, then type the program yourself and compare the output.

💡 At a Glance
PointDetails
Course AreaCore Python
Basic programming concepts used to write Python programs.
Main Usehandling wrong input
Example Fileexception-handling.py
Practice FocusRun, change values, and explain the output line by line.

Why should you learn this?

  • It is useful for handling wrong input.
  • It connects with handling missing files.
  • It improves your ability to read, write and debug Python programs.

Important Terms

These terms are used directly in this lesson. Understand them before memorising the code.

TermMeaning
tryBlock where risky code is written.
exceptBlock that runs when a selected error occurs.
elseRuns when none of the previous conditions are true.
finallyBlock that runs whether error occurs or not.
runtime errorError that happens while the program is running.

Syntax / Basic Pattern

The simple pattern is: prepare data, apply the concept, then show the result.

Basic Pattern
try:
    number = int("abc")
    print(10 / number)
except ValueError:
    print("Invalid number format")
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:

Complete Example Program

Python – exception-handling.py
try:
    number = int("abc")
    print(10 / number)
except ValueError:
    print("Invalid number format")
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:
    print("Program completed")

Expected Output

Invalid number format Program completed

Program Explanation

  • try: is part of error handling.
  • number = int("abc") stores a value in number.
  • print(10 / number) displays information or calculated result on the screen.
  • except ValueError: is part of error handling.
  • print("Invalid number format") displays information or calculated result on the screen.
  • except ZeroDivisionError: is part of error handling.
  • print("Cannot divide by zero") displays information or calculated result on the screen.

Where will you use it?

  • Handling wrong input.
  • Handling missing files.
  • Preventing sudden program crash.

Common Mistakes

  • Writing code with wrong indentation.
  • Using input() value directly in calculations without converting it to int or float.
  • Using unclear variable names that make the program difficult to understand.

Practice Tasks

  1. Type the program in exception-handling.py and run it.
  2. Change input values or sample data and observe the new output.
  3. Create one example related to handling wrong input.
  4. Write 5 lines explaining the logic in your own words.

Summary

Exception Handling is not a theory-only topic. You should be able to explain the meaning, write the example, run it successfully, and use it in a small practical program.

Exception Handling क्या है?

Exception Handling ka matlab hai: Exception handling prevents a program from crashing when an error occurs during execution. Simple words me, ye topic practical Python programs likhne me direct use hota hai.

Is topic ko sirf definition ke liye nahi, balki handling wrong input jaise real examples ke liye practice karein.

यह क्यों सीखना जरूरी है?

  • Ye handling wrong input me kaam aata hai.
  • Ye handling missing files se bhi connected hai.
  • Isse aap code ka output aur errors better samajh paate hain.

Important Terms

TermMeaning
tryBlock where risky code is written.
exceptBlock that runs when a selected error occurs.
elseRuns when none of the previous conditions are true.
finallyBlock that runs whether error occurs or not.
runtime errorError that happens while the program is running.

Syntax / Basic Pattern

Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.

Basic Pattern
try:
    number = int("abc")
    print(10 / number)
except ValueError:
    print("Invalid number format")
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:

Complete Example Program

Python – exception-handling.py
try:
    number = int("abc")
    print(10 / number)
except ValueError:
    print("Invalid number format")
except ZeroDivisionError:
    print("Cannot divide by zero")
finally:
    print("Program completed")

Expected Output

Invalid number format Program completed

Program Explanation

  • try: is part of error handling.
  • number = int("abc") stores a value in number.
  • print(10 / number) displays information or calculated result on the screen.
  • except ValueError: is part of error handling.
  • print("Invalid number format") displays information or calculated result on the screen.
  • except ZeroDivisionError: is part of error handling.
  • print("Cannot divide by zero") displays information or calculated result on the screen.

Practical Uses

  • Handling wrong input.
  • Handling missing files.
  • Preventing sudden program crash.

Common Mistakes

  • Writing code with wrong indentation.
  • Using input() value directly in calculations without converting it to int or float.
  • Using unclear variable names that make the program difficult to understand.

Practice Tasks

  1. Program ko exception-handling.py file me type karke run karein.
  2. Values change karke output compare karein.
  3. handling wrong input par ek छोटा example banayen.
  4. Logic ko apne words me 5 lines me likhein.

सारांश

Exception Handling ko tab complete maanenge jab aap iska meaning, example, output aur practical use clearly explain kar saken.

← Back to Python Tutorial