1
00:00:00,000 --> 00:00:04,990
Exceptions are a primary method of handling errors in Python.

2
00:00:05,000 --> 00:00:06,990
So let's take a look at how we handle exceptions.

3
00:00:07,000 --> 00:00:14,990
We will make a working copy of exceptions.py, call it exceptions-working.py.

4
00:00:15,000 --> 00:00:18,990
We will open our working copy and here we have a very simple little script

5
00:00:19,000 --> 00:00:23,990
that reads lines of text from a file and there is the text file there and it

6
00:00:24,000 --> 00:00:24,990
prints them on the screen.

7
00:00:25,000 --> 00:00:28,990
Go ahead and we will run this and there is our lines of text.

8
00:00:29,000 --> 00:00:30,990
You'll notice, we did this in different way this time.

9
00:00:31,000 --> 00:00:35,990
Instead of using end = a blank string, ctually using the strip method in

10
00:00:36,000 --> 00:00:40,990
the string itself and that just strips any trailing new lines from the end of the string.

11
00:00:41,000 --> 00:00:44,990
It's another way to do it, using the string method rather than using the print

12
00:00:45,000 --> 00:00:46,990
function to resolve that discrepancy.

13
00:00:47,000 --> 00:00:51,990
Now if I had typed the name wrong or if I am referring to a file that doesn't

14
00:00:52,000 --> 00:00:54,990
exist and I save this and I run it,

15
00:00:55,000 --> 00:00:58,990
you'll notice that we get one of Python's lovely little error messages and it

16
00:00:59,000 --> 00:01:01,990
has the Traceback (most recent call last).

17
00:01:02,000 --> 00:01:05,990
It's doing what it can to help you to find the error in your code and then down

18
00:01:06,000 --> 00:01:09,990
at the bottom here, it says IOError and it has a message.

19
00:01:10,000 --> 00:01:14,990
This IOError before the colon is actually the name of the exception.

20
00:01:15,000 --> 00:01:18,990
That's the exception that's being raised by the Python interpreter and then

21
00:01:19,000 --> 00:01:21,990
after the colon is its error message.

22
00:01:22,000 --> 00:01:23,990
Now I can trap that exception.

23
00:01:24,000 --> 00:01:26,990
And then I could do something different with it in my code.

24
00:01:27,000 --> 00:01:28,990
So here is how I do that.

25
00:01:29,000 --> 00:01:35,990
I use try and I am just going to put that line there in the try and then I use

26
00:01:36,000 --> 00:01:38,990
except like this, and I can print a message.

27
00:01:39,000 --> 00:01:46,990
I can say, "could not open the file."

28
00:01:47,000 --> 00:01:51,990
And then I don't want this to run, so I can put that in the else.

29
00:01:52,000 --> 00:01:57,990
Put the else there and so now what I have, I save this and run it.

30
00:01:58,000 --> 00:02:01,990
I will just get this little message, "could not open the file, come back tomorrow"

31
00:02:02,000 --> 00:02:04,990
and it doesn't try to read the lines in it.

32
00:02:05,000 --> 00:02:12,990
And if the typo is not there, I save that and run it, now I get the actual lines of text.

33
00:02:13,000 --> 00:02:17,990
Now this will actually catch any error at all, because I just said except like that.

34
00:02:18,000 --> 00:02:23,990
If I want to just kept that particular error, I say except IOError, and if I

35
00:02:24,000 --> 00:02:27,990
save that, well let's go ahead and just type this again.

36
00:02:28,000 --> 00:02:30,990
Save that, run it, then I get that error.

37
00:02:31,000 --> 00:02:35,990
So it's sometimes useful to go ahead and let Python give you that error message,

38
00:02:36,000 --> 00:02:38,990
because that's the easy way to find out which error is going to raise.

39
00:02:39,000 --> 00:02:43,990
I can also get the error message itself. I can say as e:

40
00:02:44,000 --> 00:02:48,990
like this and then instead of come back tomorrow, I can give it that error

41
00:02:49,000 --> 00:02:53,990
message like this and then save and run and it says, "Could not open the file:

42
00:02:54,000 --> 00:02:55,990
Error 2 No such file or directory:".

43
00:02:56,000 --> 00:02:59,990
And there we have something it's actually useful for the user.

44
00:03:00,000 --> 00:03:03,990
Now we can actually if we want to, we can put all of this code up here in

45
00:03:04,000 --> 00:03:07,990
the try and it doesn't hurt anything to do it that way and then we don't

46
00:03:08,000 --> 00:03:08,990
need an else clause.

47
00:03:09,000 --> 00:03:10,990
Save that and run it.

48
00:03:11,000 --> 00:03:11,990
We are still getting our error message.

49
00:03:12,000 --> 00:03:15,990
Let's go ahead and cut that out and we see that it works the same way.

50
00:03:16,000 --> 00:03:22,990
So in the event that you have more than one line of code in your try clause,

51
00:03:23,000 --> 00:03:26,990
execution will stop after the error is raised.

52
00:03:27,000 --> 00:03:31,990
So the error gets raised here and then it will go right to handling the error

53
00:03:32,000 --> 00:03:32,990
in the except clause.

54
00:03:33,000 --> 00:03:36,990
So this line won't get run when the exception happens.

55
00:03:37,000 --> 00:03:40,990
So if we have this misspelling in here, you save it and run it, we get the error

56
00:03:41,000 --> 00:03:45,990
message and it doesn't try to use the file handle that it never got, because as

57
00:03:46,000 --> 00:03:50,990
soon as that exception is raised, which happens in this open function, then the

58
00:03:51,000 --> 00:03:52,990
exception clause is run right away.

59
00:03:53,000 --> 00:03:56,990
So you'll see this pattern commonly where a number of lines of code will be put

60
00:03:57,000 --> 00:04:01,990
in a try block and then in fact sometimes there will be several different except

61
00:04:02,000 --> 00:04:06,990
clauses as well, listing out a number of different errors that could be raised.

62
00:04:07,000 --> 00:04:11,990
So this is how exceptions work in Python and this is how you handle exceptions

63
00:04:12,000 --> 00:04:22,000
in your code using try and except.

