DEV Community

Cover image for 54. Learning Python's error handling (Day 52)
Manoj Kumar
Manoj Kumar

Posted on • Originally published at emanoj.hashnode.dev

54. Learning Python's error handling (Day 52)

Today’s class delved into the intricate realm of error handling within Python. Unlike the perplexing topics of Classes, Instances, and Obstructs from Tuesday’s session, this one seemed relatively more approachable.

Error handling, I learned, doesn’t concern fixing errors within the code itself, but rather managing incorrect inputs provided by users. Consider a scenario where your program involves dividing numbers. If the user inputs alphabets, characters, or even a zero as a denominator, these entries are invalid, and it’s crucial for the program to signal an error.

Enter the saviors: the TRY and EXCEPT blocks. The TRY block lets you test a specific segment of code, while the EXCEPT block swoops in to handle any arising errors gracefully.

Basically, you write your lines of code within the TRY-EXCEPT block, like below:

try:
  num1 = int(input("Enter a number above 0: ")
except ValueError:
   print("Not a valid number. Try again")
Enter fullscreen mode Exit fullscreen mode

We studied a few other things also:

ELSE - a handy feature enabling code execution when no error occurs.

FINALLY - a reassuring block ensuring execution regardless of the TRY-EXCEPT outcomes.

The class also touched upon crafting a virtual environment and the ins and outs of uploading content to GitHub while conveniently sidestepping the inclusion of the virtual environment.

Reflecting on the day, I realize I've got a substantial amount of catching up to do. With an impending assignment due by Sunday, the pressure's on. My enthusiasm may not be at its peak, but I’m determined to dive into the study material and conquer this challenge ahead.

Time: 10:20 pm, hit the sack on a very hot night. Friday work day tomorrow!

Top comments (0)