Python class 8 error detection

Post Reply
User avatar
DevilHacker 1289
Posts: 91
Joined: Tue Nov 19, 2019 2:17 pm

Thu Jan 16, 2020 10:47 pm

We all run into them, and they can be frustrating, but there is also a silver lining to them. Today, we'll be discussing why some errors are unavoidable, and what we can do when we run into them. Also, this article will be more of a concept and less of writing actual code, but the information is equally valuable. So, let's get started!

Image

Errors Aren't Always Bad
A common misconception is that if an error occurs, you've done something wrong, this isn't always the case, sometimes errors can occur based on the user's input, or sometimes a process may fail, the point is that not all errors mean that something is broken, it just means that something needs to be accounted for. As a matter of fact, we can actually use errors to our advantage, but that is beyond the scope of this article. Now that we've conveyed the point that errors are eventually unavoidable, let's discuss how to diagnose an error.

Diagnosing an Error
When we run into an error, it will be rather obvious. I'm sure we've all seen those big chunks of text appear when we mess something up. Errors will commonly look like this...

Image

Now, since we have an error, we need to know what caused it. When we receive the error message, it will provide us with a line number, and the text that is on the offending line, a marker will also be placed at the point of failure. At the very bottom of the message, we see the type of error that occurred, in this case, it was a TypeError since we tried to print a string and an integer together. There are many kinds of errors, and these errors are organized into a hierarchy.

Expecting Errors
In order to build the most solid script possible, we need to account for every possible error. It may seem hard to spot possible error points at first, but picking them out gets easier with practice. The main thing to remember in trying to find possible points of failure is that the user will cause more errors than anything. Invalid input is one the biggest causes of errors out there, right alongside faulty code itself.

Image

The point of this section is to remember to re-read your own code, very carefully. Once you've gotten good at spotting points of failure, you'll enter a new level of scripting skill. Now that we've learned to expect our errors, let's learn how to actually code for them...

Handling Errors
Alright, enough talking about concepts, on to the code! In order to handle errors, we use to try and except. It's rather self-explanatory when you think about it, try this, and give an exception to this error. Using try and except allow us to accommodate for errors that may occur.

In order to demonstrate this, we'll be building a simple script to ask the user for a number, and repeat the number. But it will only repeat the number if the user enters a number, no words allowed. First, we'll build it without using try and except, then we'll incorporate them into our code and see the difference. So, let's start by building a version without try or except...

Image

Alright, in the above screenshot we ask the user for a number, and then we attempt to test if it is a number by attempting to add one to it. If it works, we repeat the number to the user, if it doesn't, we tell the user that they did not give valid input. Let's see what happens when we input a number, then what happens when we input a string...

Image

Image

As we can see above, we get the proper output when we give a number, but when we give a string, it returns an error. This is because we can't make an integer out of a string, so let's rebuild our script, but this time we'll use try and except...

Image

Now that we have our debugged code, let's run through it again and see if we can get it to return an error...

Image

Image

There we have it! We used to try and except in order to detect and handle errors! Let's end it by wrapping it all up!

Wrapping It Up
We covered quite a few new concepts in this article, but I hope the messages got across well. We covered the concepts of errors not being all that bad, how to diagnose an error from the error message, and how to look for areas of failure within our code. We also made a small script incorporating try and except, our error handling duo. Let's get on to the exercise!

The Exercise
Alright, now that we know how to properly deal with errors, I would like you to make and handle as many errors as you can. Set yourself up for failure, then save yourself with try and except. As always, don't be afraid to experiment!

Feedback!
That's it for this one, I hope you'll come back next time for your python's next training session!.


----HackTech----
Post Reply