DEV Community

Cover image for Day 63 of My Data Analytics Journey!
Ramya .C
Ramya .C

Posted on

Day 63 of My Data Analytics Journey!

🧠 Day 63 of My #RamyaAnalyticsJourney β€” Exception Handling in Python

Hello everyone! πŸ‘‹
Today marks Day 63 of my Data Analytics learning journey.
I explored an important concept in Python β€” Exception Handling β€” which helps make our programs more reliable and user-friendly.


πŸ’‘ What is Exception Handling?

In Python, Exception Handling is a way to manage errors that occur during program execution.
Instead of letting the program crash when an error happens, Python allows us to catch and handle those errors gracefully.

An exception is an event that disrupts the normal flow of a program.
For example, dividing a number by zero, opening a non-existent file, or converting invalid input to an integer β€” all these can cause exceptions.


βš™οΈ Why Exception Handling is Important

βœ… Prevents program crashes
βœ… Helps debug errors easily
βœ… Makes code cleaner and more professional
βœ… Improves user experience by providing clear error messages


🧩 Basic Syntax

try:
    # Code that might cause an error
    x = 10 / 0
except ZeroDivisionError:
    # Code that runs if an error occurs
    print("Error: You can’t divide by zero!")
else:
    # Code that runs if no error occurs
    print("Division successful!")
finally:
    # Code that always runs
    print("Execution completed.")
Enter fullscreen mode Exit fullscreen mode

🧠 Output:

Error: You can’t divide by zero!
Execution completed.
Enter fullscreen mode Exit fullscreen mode

🧰 Raising Custom Exceptions

We can also raise our own exceptions using the raise keyword.

age = int(input("Enter your age: "))

if age < 18:
    raise ValueError("You must be at least 18 years old.")
else:
    print("You are eligible!")
Enter fullscreen mode Exit fullscreen mode

If the user enters a number less than 18, Python will raise a custom error message.


🧾 Summary

Keyword Description
try Block where you place the code that may throw an exception
except Handles the exception if it occurs
else Executes if no exception occurs
finally Executes whether an exception occurs or not
raise Used to throw an exception manually

πŸš€ My Takeaway

Understanding Exception Handling is crucial for writing clean, safe, and professional Python code β€” especially in data analytics projects where errors can appear from data input, file reading, or API requests.

Every small concept learned builds confidence towards becoming a Data Analyst πŸ’ͺ


Python #DataAnalytics #Coding #ExceptionHandling #RamyaAnalyticsJourney #LearnToCode

Top comments (0)