Tuesday, June 17, 2025

๐Ÿ› ️ Error Handling in Python – Catching Mistakes Like a Pro!

 Have you ever run a Python program and seen a weird red error message?

That’s Python telling you something went wrong. But don’t worry! Python also gives you tools to catch and fix those errors using something called error handling.

In this blog, you’ll learn:

  • What errors are

  • Why we need error handling

  • How to use try, except, finally

  • Some fun examples to practice


❗ What is an Error?

An error is something that breaks your program.

๐Ÿ”น Two types of errors in Python:

TypeExample
Syntax ErrorForgetting a colon : in a loop
Runtime ErrorDividing by zero or accessing bad data

Example:
print("Hello)
# SyntaxError: EOL while scanning string literal x = 5 / 0 # ZeroDivisionError: division by zero

๐Ÿงฏ Why Use Error Handling?

If you don’t handle errors, your program will crash.
With error handling, you can:

  • Show friendly error messages ๐Ÿ˜‡

  • Avoid crashing the program ๐Ÿ’ฅ

  • Keep your app running smoothly ๐Ÿš€


๐Ÿงช The try and except Block

This is how we catch errors:

try:
# risky code x = 5 / 0 except ZeroDivisionError: print("Oops! You can't divide by zero.")

Output:

Oops! You can't divide by zero.

Python tried to divide, but when it hit an error, it jumped to the except block.


๐ŸŽฏ Example: User Input Error

try:
num = int(input("Enter a number: ")) print("You entered:", num) except ValueError: print("That's not a valid number!")

๐Ÿ” Using finally

The finally block always runs, whether there’s an error or not.

try:
file = open("myfile.txt", "r") content = file.read() except FileNotFoundError: print("File not found!") finally: print("This runs no matter what.")

๐Ÿ”„ Catching Multiple Errors

try:
x = int(input("Enter number: ")) y = 10 / x except ValueError: print("That's not a number!") except ZeroDivisionError: print("Can't divide by zero!")

๐Ÿง  Pro Tip: Use else for clean execution

try:
age = int(input("Enter your age: ")) except ValueError: print("Please enter a number.") else: print("Your age is:", age)

๐Ÿšจ Common Errors in Python

Error TypeDescription
ValueErrorWrong data type (e.g. text instead of number)
ZeroDivisionErrorDividing by zero
IndexErrorList index out of range
KeyErrorMissing key in dictionary
TypeErrorWrong operation on data types
FileNotFoundErrorMissing file

✅ Summary

Python’s error handling tools (try, except, else, and finally) help you write safer, more professional code.

  • Start with try and except

  • Use finally for cleanup

  • Catch specific errors to give clear messages


๐Ÿงช Practice Challenges

  1. Write a program that divides two numbers and catches divide-by-zero error.

  2. Ask the user to enter a file name. Catch FileNotFoundError.

  3. Build a calculator that handles wrong input gracefully.


๐Ÿš€ Keep Learning!

Error handling is just the start. As you write bigger programs and games, you’ll use it all the time.

Keep coding, keep crashing, and keep fixing! That’s how you become a pro! ๐Ÿ’ป๐Ÿ”ฅ


No comments:

Post a Comment

๐ŸŽฎ Build Your Own Tic Tac Toe Game Using Python – For Beginers

 Do you love playing Tic Tac Toe ? Today, we’ll show you how to build your very own Tic Tac Toe game in Python — no advanced skills needed!...