1 00:00:00,720 --> 00:00:03,320 - [Instructor] In some cases, you might find it necessary 2 00:00:03,320 --> 00:00:07,770 to raise exceptions explicitly in your code. 3 00:00:07,770 --> 00:00:11,410 For example, if you are defining your own functions that 4 00:00:11,410 --> 00:00:14,720 are going to be part of a library that other people use, 5 00:00:14,720 --> 00:00:17,500 you may want to indicate exceptions 6 00:00:17,500 --> 00:00:20,940 when they use your library's functions incorrectly. 7 00:00:20,940 --> 00:00:24,520 So to do that, Python provides the raise statement 8 00:00:24,520 --> 00:00:28,180 and you simply follow the raise keyword by the name 9 00:00:28,180 --> 00:00:32,210 of the exception class that you want to create an object of 10 00:00:32,210 --> 00:00:37,030 and insert into the exception handling mechanism. 11 00:00:37,030 --> 00:00:40,940 Now optionally, when you're creating that exception object, 12 00:00:40,940 --> 00:00:44,710 you can supply parenthesis following the class name. 13 00:00:44,710 --> 00:00:48,850 So for example, if you wanted to give a custom error message 14 00:00:48,850 --> 00:00:52,020 for a particular exception instance that you're creating, 15 00:00:52,020 --> 00:00:55,060 you would supply that as an argument in parenthesis 16 00:00:55,060 --> 00:00:59,710 following the exception class name in your raise statement, 17 00:00:59,710 --> 00:01:02,000 and by the way, before you raise an exception, 18 00:01:02,000 --> 00:01:05,610 it's important that your code de-allocate any resources 19 00:01:05,610 --> 00:01:09,040 that it acquired so those resources 20 00:01:09,040 --> 00:01:11,650 are handed back to the system properly. 21 00:01:11,650 --> 00:01:14,210 Now in general, you'll probably work with 22 00:01:14,210 --> 00:01:17,090 one of the many built-in exception types. 23 00:01:17,090 --> 00:01:18,810 You can find those at the URL 24 00:01:18,810 --> 00:01:21,130 shown at the bottom of this slide, 25 00:01:21,130 --> 00:01:24,730 but it is possible to create your own new exception types 26 00:01:24,730 --> 00:01:28,870 as well which does require creating new class definitions. 27 00:01:28,870 --> 00:01:32,303 That's part of what we'll be discussing in the next lesson.