Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
See how each error can be handled using Python's try/except blocks.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back, ready to see
the solution that I came up with?
0:00
The first thing I'm going to do is I'm
actually going to comment out which is
0:05
command slash everything down below, so
that I can handle one error at a time.
0:11
And I'm going to move up the terminal
a little bit, do python3 app.py, and
0:16
you can see this is the error I'm going
to get for what's happening on line 3.
0:22
It's going to tell me value error, and
invalid literal for int with base 10 tree.
0:28
So in here, I'm gonna do try,
0:36
which says I want you computer to try and
0:39
run what's on line 4 here.
0:44
And if you can't run it,
0:47
I'm gonna do except then I
want you to give a message.
0:50
Seem like I might have spelt that wrong,
but doesn't matter.
1:03
Okay, now I'm going to run the file again.
1:08
And you can see now instead of this really
long traceback here in the console,
1:11
I'm just getting an error occurred,
which tells me that an error happened.
1:17
Right now my code is obviously very small,
so
1:23
I could figure out what happened and
fix it.
1:25
But if you were having
a much larger programme,
1:29
having something that just says an error
occurred, probably not as helpful.
1:32
So let's try something
else here on number 2.
1:37
I'm going to uncomment it,
1:41
and now that the first one is
working I can run the file.
1:43
Oops, I forgot to hit Save CTRL + S, or
1:49
Command S depending on what
computer you're on, there we go.
1:52
So you can see we still
get an error occurred,
1:56
which is happening because
of number one up here.
1:59
And then I'm getting a new error for
number 2, which is saying
2:04
that we still have that value error
because of an invalid literal.
2:08
Because it's all- one trying to
convert a string to an integer, but
2:14
the string is also a float and
so it's causing us issues.
2:18
So let's try something a little
different with this one.
2:21
We still need to start with our try.
2:25
And we need to tell the computer
I want you to try doing this,
2:27
what's now on line 10, except I'm going
to put here and accept a value errors.
2:33
So this is going to specifically work for
value errors, which we know is the error
2:41
that we're getting because that's
what we have here in the console.
2:46
So if we get a value error,
it's going to automatically give us this
2:50
message that you see
down here in the console.
2:54
And we can save that as a variable.
2:58
So, the value error
that you are giving us,
3:00
I want you to give it to us as this
variable called Error, or err.
3:04
And then i'm gonna print an F string here,
3:09
that's going to say the same thing,
an error occurred.
3:14
And we're gonna put the error
inside of our string.
3:24
Save, and
now let's run this in the console.
3:29
I'm gonna run a clear, I'm gonna do up
arrow to run the file again, perfect.
3:31
So now you can see we get an error
occurred for our first error.
3:37
And then the second one we
get an error occurred, and
3:41
then it gives us the rest of the
information that we saw in the console.
3:44
This information that we have here
is super valuable for other coders.
3:49
But if you're passing
these errors to a user,
3:54
that information is
probably not very helpful.
3:58
So let's try something
different here on number 3.
4:02
So we have a list and when we access
the fifth index of that list,
4:08
it's going to be whatever should come
next, which doesn't exist currently.
4:13
So I'm going to put the try right here,
4:19
tab this over so
it's inside of my try block.
4:23
Because this my list is not
going to cause an error.
4:28
But on line 17, when I'm trying
to access an incorrect index,
4:31
that will give us an error and
actually before I do that, Save.
4:36
And let's run it in the console swing,
see what that error is.
4:40
Okay, here we go.
4:43
We get an index error,
the list index is out of range.
4:46
So, let's put our try here, tab this over.
4:53
I'm gonna do an except to do
5:02
our capital I index error.
5:07
And inside I'm in print, do an F string,
5:12
and I'm gonna tell them
the index out of range.
5:17
And then I'm gonna let the user
know what is available.
5:23
So I'm gonna do available
range always starts at 0,
5:27
because indexes are 0 noted.
5:33
And then we're going to go to,
we put in our brackets, so
5:36
I can do a little programming in here.
5:40
The length of our list, which is my list,
and then we need that minus 1.
5:43
Because remember,
when you do length of a list,
5:49
it's going to count how many items
are in the list, which will be 5.
5:52
But our indexes start at 0,
so it'd be 0,1,2, 3, 4.
5:56
So it's gonna be the length
of your list minus 1.
6:03
Scoot this over a little bit so
you can see the whole thing, and
6:10
then don't forget to save.
6:13
Run it down here in the console.
6:15
And now you can see our first error,
an error occurred, our second error,
6:17
an error occurred, and
then our error statement.
6:21
And then the third error,
we get index out of range.
6:24
And then we let the user know
available range is 0 to 4.
6:26
So a lot more helpful for a user and
it could also be helpful for
6:31
someone who's using your code
in their own program as well.
6:35
Now let's look at number 4.
6:40
Okay, so this input here we're we're
asking the user to input some information.
6:49
It's probably not gonna give us an error,
6:55
but this conversion could, and
let's see how that might work.
6:58
Oops, forgot to save again.
7:03
Now let's run the file, and
you can see at the bottom,
7:06
it's asking how many hats do I own?
7:08
Well, if I mistype, and I just give a
letter like the letter T, it's gonna give
7:10
us that same value error that we've been
seeing before with these conversions.
7:14
And we don't want that to happen.
7:19
So let's handle this error try,
tab in the thing we want our computer
7:23
to try that we know is probably gonna give
an error, or just could give an error.
7:28
And then I'm gonna except ValueError, and
7:34
then I'm gonna print a more
understandable message.
7:39
You must enter a number, and
7:44
you could even be more clear.
7:48
You could say you must enter an integer,
7:53
that way they don't give
you a float of 15.99.
7:56
So if I run the file again, you can see
I get all of our error messages, and
8:02
then I get how many hats do you own?
8:07
If I put in some random words or letters,
8:09
you can see it gives me the error,
you must enter an integer.
8:13
So now we've tackled all
of the errors in this file.
8:18
And I purposely did them each
a little bit different to so
8:22
you can see the different ways
that you can handle an error.
8:25
I know error handling can
be difficult at first, but
8:31
I hope this practice session has helped.
8:34
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up