Python Part 13 - Error-trapping Office

Python Part 13 - Error-trapping Office So welcome to this wisel tutorial on error trapping in python here's what you'll learn during the tutorial so we'll begin by looking at an error.

Example easy enough to create and then we'll look at how you can do error trapping basic error trapping in python before looking at specific error trapping so trapping things like divide.

By zeros or type errors we'll then look at how errors bubble up from functions into calling code and then we'll look at how you can return an error message from a function.

Which turns out to be a bit harder than you might think we'll look at how you can raise your own errors as if built-in errors weren't enough and why you might do this.

And then for completing the sake we'll look at the else and the final clauses just to show everything which is possible and finally we'll look at the rather.

Heretical question of whether error trapping is actually worth it and what the alternatives are at the top right of your screen about now a link should appear and you can.

Python Part 13 - Error-trapping

Click on that to download any files to do with this tutorial if it doesn't appear you can get them from the youtube site there's a link there too but as always i'm uncomfortable being on.

Screen particularly with this jungle behind me so what i'm going to do now is to disappear and hand control over to the everloyal sven who will guide you through the rest of this tutorial.

So let's get started before we look at error trapping we need to actually be able to generate an error to trap so what i've done is i've created a.

Folder called error trapping and a file called badcoding.py which gives you some idea of the quality of the program we are about to write so what i'm going to do is we're going.

To create a function to calculate the reciprocal of a number and i'll call it reciprocal and it will take in a number.

And let's say we can have any floating point number and it will spit out another float we hope and then within the function what it.

    Will do is return one divided by the

    Number and any programmer should immediately start having i know it's like someone walking over your grave you just don't do one divided by a number.

    Without first checking for divide by zero errors so let's see if it works if i print out the reciprocal of let's say 10 that should give me 0.1.

    Because that's one over it and it does 0.1 so that's great let's try the reciprocal of zero one divided by zero gives an error.

    Specifically it gives a zero division error there's different types of errors generated by python and that's one let's look at another let's try taking cheeseburger as the input.

    And that will give me a type error so i've managed to create a function which actually allows at least two possible types of errors and what we're now going to do is we're going to trap.

    For that and before we do that let's look at the syntax of error trapping we're looking at the syntax of error trapping we've got here a dodgy statement it's trying to print out one.

    Divided by zero to error trap something you precede it or you proceed the dodgy thing you're trying to do with the word try followed by a colon.

    If i was to run this program as it stands it would give me an error message because try has to be accompanied by accept and in the accept clause you take some.

    Remedial action whatever that may be in this case it might be printing out a message saying that you can't divide by zero there are other words you can use like.

    Else and finally which we'll come to

    Towards the end of this tutorial and you can also test for different types of errors but that's the basics of it so let's put this into practice in our.

    Code what we'll do is add some error trapping before our offensive or potentially offensive statement later on we'll see that it might be.

    Better to to do this within the function but for the moment we'll just put it in the main body of the program so to do this uh we'll trap possible errors.

    And to do that i can just put the word trying followed by colon and then need to indent everything which follows it and i need to put in an accept clause and in the accept clause i can say what.

    Happens if things go wrong so i'll just print out in this case something went wrong which is a nice vague error message so if i try running that you can see i.

    Get the message something went wrong because you can't take the reciprocal of a cheeseburger but you can take the reciprocal of 42. so if i run this again now it will give me the reciprocal which.

    Apparently is that number so that's basic error trapping let's look now at how you can trap different types of errors so just before we look at catching uh.

    Specific types of errors a quick reminder of what the areas we might generate are here i'm trying to print out one divided by zero if i run this program i will get.

    A zero division error if i comment that line of code out and instead try to divide one by a bit of text then i will get a type error so those are the two specific areas.

    We're going to trap and to do that you can change the word accept and after it type in the specific type of area you're trying to trap i wanted to divide by zero error i can't.

    Already remember what that was called but if i type in div providing i don't type any more than that you can see zero division error is one of the possibilities in the list so.

    It's trying to help you so i'll choose that and put a colon after it and if there's a zero division error i'll print out a message saying can't.

    Divide by zero otherwise the next type of error i'm going to trap is a type error and this one i can remember.

    And if that's the case i will put a message saying make sure you use a number now if i get to this point i know i.

    Didn't have a zero division error and i didn't have a type error either but it's still possible my program could have crashed so i'll just make sure by putting in a general accept clause to.

    Mop up all other possibilities in this case i'll say something else went wrong at the end of this tutorial i'll come to a question whether error trapping is is actually worth doing or not.

    A very heretical question so let's try running that if i run that at the moment it will give me the reciprocal because there's nothing wrong with taking the reciprocal of 42. if i.

    Change that to zero i'll get can't divide by zero and if i change that to a bit of text like bob i'll try running that it will give you make sure you use a number i can't think of a way of.

    Generating any other type of error so i don't think i'm going to be able to test my final statement that's how you can test for specific types of error.

    All of the examples we've done so far have used a concept called error bubbling the error which is happening is occurring within the function itself.

    That's where it's crashing but because that doesn't handle the error it's bubbling up it's being raised up to the calling function unfortunately that does handle it.

    Now there's a good argument for saying you should always handle errors at the level at which they occur the problem with the approach i've taken so far is that while this is.

    Well-behaved code some other less enlightened person might call my reciprocal function and not handle the error which occurs and in that case it will just crash.

    Whereas if i had the error trapping occurring within the function i wouldn't have that problem so i've created a separate file called no error bubbling dot py.

    In which i'm going to try to show you this principle and talk through some of the issues involved so i've created my function within that and i'm going to add error trapping to.

    It which you do in exactly the same way so i'll put a try statement in and if it's all good i'll return one divided by the number and then i'll have a general accept.

    Statement so i won't bother catching the specific type of error but the question then is what do i do if it crashes what do i return.

    I guess what i would like to do is return an error message but i've already said that this is returning a float and i don't really want to make lose that strong typing.

    So instead what i'll do is print out an error message here saying what my problem is which is that i i cannot create a reciprocal.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=mNCS-VwzljY
Previous Post Next Post