You are on page 1of 2

1

00:00:00,000 --> 00:00:04,990


Exceptions are Python's key method for handling errors.

2
00:00:05,000 --> 00:00:08,990
Whenever you see one of Python's little error messages, like you've written some

3
00:00:09,000 --> 00:00:13,990
script and you run it for the first time and it dumps you out with stack trace

4
00:00:14,000 --> 00:00:19,990
and an error message, those error messages are simply uncaught exceptions.

5
00:00:20,000 --> 00:00:23,990
You can catch exceptions in Python using try and except.

6
00:00:24,000 --> 00:00:28,990
For example, if you're opening a file and the file name is wrong or you don't

7
00:00:29,000 --> 00:00:32,990
have permissions to open the file or something like that, Python will raise

8
00:00:33,000 --> 00:00:34,990
an IO Error exception.

9
00:00:35,000 --> 00:00:38,990
You can catch the exception like this, using try and except.

10
00:00:39,000 --> 00:00:42,990
Then you can even capture Python's error message, print it, and either continue

11
00:00:43,000 --> 00:00:47,990
with your execution or give a user some intelligible error message or whatever

12
00:00:48,000 --> 00:00:50,990
exactly it is that you want to do with the error.

13
00:00:51,000 --> 00:00:54,990
And then you can use else for conditions where you don't get the error at all,

14
00:00:55,000 --> 00:00:56,990
and it just works the way that you expect it to.

15
00:00:57,000 --> 00:00:59,990
Of course, you can also raise your own exceptions with the raise statement.
16
00:01:00,000 --> 00:01:05,990
You have access to this entire exception handling process. It's built into

17
00:01:06,000 --> 00:01:10,990
Python for your own error conditions in the modules, objects, and functions

18
00:01:11,000 --> 00:01:11,990
that you write yourself.

19
00:01:12,000 --> 00:01:22,000
So let's go ahead and take a look at how Python uses exceptions for its error
reporting.

You might also like