Exception Handling in Python
Python Made Easy - (Article 13) Telugu Explanation
1. Introduction - What is an Exception?
Suppose nenu program lo divide chesthunnanu
a = 10
b = 0
print(a / b)Output:
ZeroDivisionError: division by zeroProgram crash ayipoyindi!
Ila unexpected errors ni Exceptions antaru.
So, Exception Handling ante - “error vasthe program crash kakunda handle cheyyadam”.
2. Why We Need Exception Handling?
Without exception handling,
program stop aipothundi → next code execute avadhu.
With exception handling,
manam “error vache chance unna code” ni try chestham,
and “error vaste” manam neat ga handle chestham.
3. Basic Syntax of try - except
try:
# risky code
except:
# handle errorExample:
try:
a = 10 / 0
except:
print("Something went wrong!")Output:
Something went wrong!Explanation:
tryblock lo error vache code untundi.exceptblock error vasthe execute avuthundi.- So program crash avvakunda safe ga run avutundi.
4. Handling Specific Exceptions
Manam specific error type ni handle cheyyachu
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Please enter a valid number!")Output examples:
- If user gives
0→Cannot divide by zero! - If user gives
"abc"→Please enter a valid number!
Advantage:
Different errors ni different ways lo handle cheyyachu.
5. Using else Block
else block eppudu execute avuthundi ante, only if try block lo error raledu annapudu.
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero!")
else:
print("Division successful!")Output:
Enter a number: 5
Division successful!Explanation:
- Error raledu →
elseblock execute ayindi.
6. Using finally Block
finally block always execute avuthundi — error vasthe kuda, raani kuda.
Get D Naresh’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
try:
file = open("data.txt", "r")
print(file.read())
except FileNotFoundError:
print("File not found!")
finally:
print("Closing file...")Output:
File not found!
Closing file...Explanation:finally block lo usually cleanup actions (like closing file, disconnecting DB) chestam.
7. Nested try - except Example
try:
a = int(input("Enter number: "))
try:
print(10 / a)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Please enter a valid integer!")Explanation:
- First inner block division try chesthundi
- Outer block input type check chesthundi
8. Raising Custom Exceptions
Sometimes manam custom error message create cheyyali ante raise keyword vaadali.
Example:
age = int(input("Enter your age: "))if age < 18:
raise Exception("You must be 18 or older!")
else:
print("Welcome!")Output (if age 15):
Exception: You must be 18 or older!9. Creating Your Own Exception Class
Custom class create cheyyadam advanced but easy
class InvalidAgeError(Exception):
passtry:
age = int(input("Enter your age: "))
if age < 18:
raise InvalidAgeError
else:
print("Valid age!")
except InvalidAgeError:
print("Age must be 18 or above!")10. Key Takeaways
Main idea:
Exception handling → program safe & professional ga nadustundi, crash kakunda!
11. Practice Questions
1️.Divide two numbers input ga tiskoni handle all errors (ZeroDivisionError, ValueError).
2️.Try to open a file that doesn’t exist — handle error neatly.
3️.Create a custom exception for “invalid password”.
4️.Use finally block to print “Program Completed” every time.
Previous & Next :
Next Article:👉 14. Modules & Packages in Python
Previous Article:👉 12. File Handling in Python









