1
00:00:00,000 --> 00:00:03,990
Error reporting in Python is done with something called exceptions.

2
00:00:04,000 --> 00:00:08,990
Exceptions are an object- oriented way of handling errors.

3
00:00:09,000 --> 00:00:11,990
So let's take a look how this works in Python.

4
00:00:12,000 --> 00:00:16,990
Go ahead and make a working copy of exceptions.py. We'll call it

5
00:00:17,000 --> 00:00:18,990
exceptions-working.py.

6
00:00:19,000 --> 00:00:25,990
So let's go ahead and open our working copy, and we will notice that we have a

7
00:00:26,000 --> 00:00:29,990
script here that opens a file and reads the lines and prints the lines.

8
00:00:30,000 --> 00:00:34,990
The problem is is that it's spelled the filename wrong, and so when we run this,

9
00:00:35,000 --> 00:00:37,990
we will get an error, and we see there is the error.

10
00:00:38,000 --> 00:00:41,990
And you will notice that error is in red. That's because Python sends its default

11
00:00:42,000 --> 00:00:46,990
error messages out to standard error, which is printed in red in Eclipse, but

12
00:00:47,000 --> 00:00:50,990
what we want to do is want to be able to catch that error and do something

13
00:00:51,000 --> 00:00:52,990
intelligent within our scripts.

14
00:00:53,000 --> 00:00:58,990
We are going to do this with 'try,' and we will go ahead and indent this code

15
00:00:59,000 --> 00:01:01,990
under 'try' and accept.

16
00:01:02,000 --> 00:01:09,990
Now we will just print, 'something bad happened,' and then we can go ahead and

17
00:01:10,000 --> 00:01:18,990
continue to execute and say print 'after badness.' And so when we run this, go

18
00:01:19,000 --> 00:01:23,990
ahead and save it and run it, something bad happened and after badness.

19
00:01:24,000 --> 00:01:29,990
We can see that we caught the error, we did something, and we continued after that.

20
00:01:30,000 --> 00:01:33,990
Now we actually print the error message that Python gives us. We can say

21
00:01:34,000 --> 00:01:40,990
except IOError as e and I can say something bad happened and put the error

22
00:01:41,000 --> 00:01:43,990
message in parentheses here.

23
00:01:44,000 --> 00:01:51,990
.format (e)), and when I save that and run it, something bad happened (Error 2 No

24
00:01:52,000 --> 00:01:52,990
such file or directory,

25
00:01:53,000 --> 00:01:59,990
and now we know exactly what the problem was. We could set a flag. We could mere

26
00:02:00,000 --> 00:02:03,990
some different code. We can do anything different that we want to. We have the

27
00:02:04,000 --> 00:02:09,990
full reporting of Python's errors as this variable e

28
00:02:10,000 --> 00:02:11,990
So this is how Python handles errors.

29
00:02:12,000 --> 00:02:16,990
It throws these exceptions, and this is how we catch them and how can do

30
00:02:17,000 --> 00:02:17,990
something intelligent with them.

31
00:02:18,000 --> 00:02:28,000
We will see a lot more examples of this as we go through the rest of the course.

