Debugging in python using the "raise statement"

Debugging in python using the "raise statement"

Debugging involves identifying, understanding and resolving possible causes of error in your code, as a programmer, the occurrence of bugs is inevitable. Whether you are a beginner or a professional programmer, the tendency of writing codes that results in error still exists.

In this blog article, we would be looking at how to identify the causes of bugs(errors) in your program code and how to manage or handle resulting errors.

According to Wikipedia , Debugging in computer programming and software development is the process of finding and resolving bugs (defects or problems that prevent correct operation) within computer programs, software, or systems.

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions

Python raises an exception whenever it encounters incorrect code during program execution.

In my previous article,"Catch the bugs", I wrote about using the try and except statement to handle possible exceptions(errors). You can also raise your own exceptions in your code in a more understandable format you desire using the raise statement.

The raise statement

The raise statement is usually used within a function definition and the try and except statements to handle errors resulting within the program code.

In a program without the try and except statement, on program execution, once the python interpreter encounters a raise statement, it automatically crashes the program and display the message passed to the exception() function as string, otherwise, it stops the program execution and moves to the except statement. We would look at examples on this later in this article.

How to raise exceptions using the "raise" statement

In order to define your own ways of handling possible errors in your code using the raise statement, the raise statement must consist of;

  • The raise keyword
  • An Exception() function call
  • Your desired error message passed as a string to the Exception() function.

As mentioned earlier, the try and except can be used with the raise statement.

Now let us go further to see some implementations of this.

Simple Implementation of the raise statement

Like I mentioned in the raise statement section above, raise statements are usually used within a function. Let us see how this is done;

Suppose, a mall has a rule for packaging customer purchased items, such that, only 6 chocolates, 4 burgers and 5 cupcakes can be put in a shopping bag.

We would define a function that takes in the number of chocolates, burgers and cupcakes to be put in a shopping bag and raises an exception where they are more than 6,4,5 respectively.

For these examples, I would be using Vs-code and python 3.8.2.

In your choice editor, enter the following (you can use your preferred message with the Exception calls);

def bagChecker(chocolates, burgers, cupcakes):
    if chocolates > 6:
        raise Exception('You have exceeded the number of chocolates for one shopping bag')
    if burgers > 4:
        raise Exception('You have exceeded the number of chocolates for one shopping bag')
    if cupcakes > 5:
        raise Exception('You have exceeded the number of chocolates for one shopping bag')

Now call this function and pass the following values as argument, and run your program.

bagChecker(4, 7, 9)

Your output would be;

pic_one.png

Now let us put this bagChecker() function within the try and except statement,

try:
    bagChecker(4, 7, 9)
except Exception as error:
    print(str(error))

Your output would be;

pic_two.png

Notice, the difference in the presentation of the error message, for the first example, the code crashed, then raised the exception, that resulted in the output as seen in example 1.

While, in the second example, the program execution in the try statement on encountering an exception, stopped and moved over to the except statement passed the error message in the exception() into 'error', then the error message was displayed in a simple and straight-forward format using the print statement.

From this, you can see that inserting your raise statement within a try and except statement provides;

  • A more clarified and straight-forward way of identifying causes of program errors with personalized error messages other than the default python error message.
  • Handles errors more grace-fully instead of letting the entire program crash.

Thanks for reading.

I hope this was resourceful.

Drop a like and/or a comment.