Bugs everywhere! Catch them All

Bugs everywhere! Catch them All

When writing codes, there are usually times when an error occurs and identifying the cause could be very tasking, you might as well as spend a good amount of time, checking through but might not find anything after all. In the end, you might get help, then you realize it was something you knew could have easily identified an error.

When I started coding, I remember a time when I had a bug that took me two days to find and when I did, it was a semicolon at the end of my for loop.

The big question now is, 🤨 How did I skip that?

Let's say 🤔

  • The lengthy error message
  • You were tensed
  • Anxiety
  • You taught you know every detail of the code,(sure!, you did write those codes).

You definitely are not experiencing anything different, someone else has.

So, why not find a way to catch the bugs, you know might occur.

How do I then catch these errors(bugs), and make the errors understandable to anyone?🤔

In this article, I would be show you through on how this can be implemented in a simple Python program using the TRY and EXCEPT clauses.

Let’s get started;

Now get your devices on which you run your python codes handy, if you don’t have a device, there are numerous online python IDE you can utilize, I’d recommend, onlinegdb.com/online_python_compiler.

Using a simple division problem as an example,

create a new file and enter the following lines of code, then save and run;

def divise(a,b):    #initialization of the function to divide two numbers
    return a/b    #return value gotten from division

print(divise(6,0))    #print value returned by the function call and input of values for variables,’a’ and             ‘b’
print(divise(18,2))
print(divise(25,5))

when this code is executed, you would notice that it’s execution was halted, as soon as the interpreter met with an error in the first print statement which resulting from dividing 6 by 0.

We don’t want our program execution to be interrupted by bugs, that might take time understanding and not to mention, bugs we know are likely to happen. So what now.

When you inputted zero as a divisor, it might have skipped your memory that zero as a divisor would result in a RUNTIME ERROR,ZeroDivisionError,(this was exactly the error given by the interpreter).

Let’s get our bugs using a TRY and EXCEPT clause to catch subsequent errors like this, by putting potential error resultant codes in the TRY clause;

  • Create a new file and enter the following lines of code, then save and run
def divise(a,b):
    try:
        return a/b    

    except ZeroDivisionError:
        print(‘Look again, you have entered zero(0) as a divisor’)



print(divise(6,2))    
print(divise(18,0))
print(divise(25,5))
  • Go ahead, execute the code, now you can see that when the interpreter got to the point where zero is the divisor, a detailed text was printed out. This helps you know what actually happened and what caused the error in simple understandable terms .

You can also put your function calls in the TRY and EXPECT clauses, again create a new file and enter these codes:

def divise(a,b):    
    return a/b    
try:
    print(divise(6,0))    
    print(divise(18,2))
    print(divise(25,5))

except ZeroDivisionError:
    print(‘Look again, you have entered zero(0) as a divisor’)
  • You would notice that subsequent lines of codes were not executed, this is because, when the interpreter enters the EXPECT clause, it does not go back to the TRY clause, but continues downwards to the end. Change the print statement with the divisor to the last place and see that all codes are executed, you can play around with this.

Now you got your bug in a container, you can easily locate and fix the error.

💪Kudos to you, Great Job!.

If you found this interesting, feel free to drop a comment, share with your contacts and you can also sign up to my newsletter for more interesting content like this.