1
00:00:00,000 --> 00:00:03,990
Python uses exceptions as its primary method of handling errors.

2
00:00:04,000 --> 00:00:07,990
We can raise our own exceptions in the functions and modules that we write.

3
00:00:08,000 --> 00:00:09,990
Let's take a look at how we do that.

4
00:00:10,000 --> 00:00:16,990
Make a working copy of exceptions.py, call it exceptions-working.py, and we'll go

5
00:00:17,000 --> 00:00:20,990
ahead and open our working copy, and here we have our simple print the lines of

6
00:00:21,000 --> 00:00:23,990
text from this file. Go ahead and run that.

7
00:00:24,000 --> 00:00:28,990
Now, let's say that we want it to write a function that will open a file and

8
00:00:29,000 --> 00:00:30,990
return its lines of text.

9
00:00:31,000 --> 00:00:38,990
It's pretty easy little function, call it readfile, and we'll pass it a filename

10
00:00:39,000 --> 00:00:51,990
and open(filename), like that, and we'll return the readlines method from the

11
00:00:52,000 --> 00:00:58,990
filehandle and that's actually an Iterator and it allow us to do this. We can

12
00:00:59,000 --> 00:01:10,990
now get rid off that and here we can say readfile lines.text. Save that and run it

13
00:01:11,000 --> 00:01:13,990
and there it works exactly like we expect.

14
00:01:14,000 --> 00:01:20,990
Now, let's say that we misspell lines.text here. We'll get our same error

15
00:01:21,000 --> 00:01:39,990
message that we got before, IOError, and of course we can handle that in a try block like this.

16
00:01:40,000 --> 00:01:45,990
And there we have our error handling, our exception catching,

17
00:01:46,000 --> 00:01:48,990
and there we get our error message like that, but let's say that we have another

18
00:01:49,000 --> 00:01:52,990
condition that we want to raise a different kind of an error for.

19
00:01:53,000 --> 00:01:59,990
We could check the filename for example, using the endswith method of the string

20
00:02:00,000 --> 00:02:06,990
object to see if it ends with .TXT. And in that case, we'll just go ahead and do

21
00:02:07,000 --> 00:02:10,990
what we normally do, an else. We're going to raise an exception.

22
00:02:11,000 --> 00:02:15,990
We're going to use the raise statement, we're going to raise a ValueError and we're going to

23
00:02:16,000 --> 00:02:24,990
give it a little message, "Filename must end with .TXT," all right and then give

24
00:02:25,000 --> 00:02:26,990
it a different kind of a name here.

25
00:02:27,000 --> 00:02:29,990
We save this and run it.

26
00:02:30,000 --> 00:02:31,990
Now, we have an unhandled exception.

27
00:02:32,000 --> 00:02:36,990
We get exactly the same kind of error reporting that Python does when it raises

28
00:02:37,000 --> 00:02:40,990
its own exceptions and we see it gives us the trace back.

29
00:02:41,000 --> 00:02:42,990
It's just an unhandled exception.

30
00:02:43,000 --> 00:02:45,990
That's what Python does with an unhandled exception.

31
00:02:46,000 --> 00:02:51,990
And so, we can handle that exception with a separate except, except ValueError

32
00:02:52,000 --> 00:02:59,990
as e and we can print bad filename and the message.

33
00:03:00,000 --> 00:03:04,990
So now when we save it and run it, we get this nice little error message here.

34
00:03:05,000 --> 00:03:06,990
So that's how we raise an exception in Python.

35
00:03:07,000 --> 00:03:12,990
You'll notice it's very simple. We use the raise keyword and we give it the exception.

36
00:03:13,000 --> 00:03:18,990
Python has predefined a number of exceptions in its library, pretty much

37
00:03:19,000 --> 00:03:23,990
anything you are going to need, and on your screen is the URL where you can find

38
00:03:24,000 --> 00:03:25,990
that list with a lot of descriptions of them.

39
00:03:26,000 --> 00:03:29,990
In this way you don't need to define your own and your programs will work more

40
00:03:30,000 --> 00:03:34,990
consistently with the rest of the Python universe, by using the exception names

41
00:03:35,000 --> 00:03:45,000
that are already defined.

