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.
| Point | Details |
|---|---|
| Course Area | Core Python Basic programming concepts used to write Python programs. |
| Main Use | handling wrong input |
| Example File | exception-handling.py |
| Practice Focus | Run, 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.
| Term | Meaning |
|---|---|
| try | Block where risky code is written. |
| except | Block that runs when a selected error occurs. |
| else | Runs when none of the previous conditions are true. |
| finally | Block that runs whether error occurs or not. |
| runtime error | Error that happens while the program is running. |
Syntax / Basic Pattern
The simple pattern is: prepare data, apply the concept, then show the result.
try:
number = int("abc")
print(10 / number)
except ValueError:
print("Invalid number format")
except ZeroDivisionError:
print("Cannot divide by zero")
finally:Complete Example Program
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
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
- Type the program in
exception-handling.pyand run it. - Change input values or sample data and observe the new output.
- Create one example related to handling wrong input.
- 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
| Term | Meaning |
|---|---|
| try | Block where risky code is written. |
| except | Block that runs when a selected error occurs. |
| else | Runs when none of the previous conditions are true. |
| finally | Block that runs whether error occurs or not. |
| runtime error | Error that happens while the program is running. |
Syntax / Basic Pattern
Basic idea: pehle data तैयार करें, phir Python logic apply करें, aur finally result display करें.
try:
number = int("abc")
print(10 / number)
except ValueError:
print("Invalid number format")
except ZeroDivisionError:
print("Cannot divide by zero")
finally:Complete Example Program
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
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
- Program ko
exception-handling.pyfile me type karke run karein. - Values change karke output compare karein.
- handling wrong input par ek छोटा example banayen.
- 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.