1 00:00:00,480 --> 00:00:01,100 Hello. 2 00:00:01,230 --> 00:00:05,170 Welcome to the session on errors and exceptions. 3 00:00:05,280 --> 00:00:09,870 There are two types of arrows that typically occur when writing programs. 4 00:00:10,840 --> 00:00:20,880 Syntax errors and exceptions syntax errors occurred due to incorrect structure of the program. 5 00:00:20,880 --> 00:00:29,350 This means that the programmer has made a mistake in the structure of a statement or an expression even 6 00:00:29,370 --> 00:00:29,910 lousy. 7 00:00:29,910 --> 00:00:32,710 An example in the Jupiter note book. 8 00:00:33,000 --> 00:00:42,480 In this example here I have a print function that in an if statement and when we executed this line 9 00:00:43,050 --> 00:00:47,180 Python interpreter has thrown a syntax error. 10 00:00:47,520 --> 00:00:55,010 And if you notice in the output the line where the error was detected is displayed. 11 00:00:55,290 --> 00:01:05,250 And also we have a little arrow here pointing at the earliest point in the line where the error was 12 00:01:05,250 --> 00:01:05,910 detected. 13 00:01:07,470 --> 00:01:16,890 And if you notice carefully the colon that should follow after the next statement is missing and apart 14 00:01:16,890 --> 00:01:26,970 from the error messages and the line where the error was detected in the output the file name and a 15 00:01:26,970 --> 00:01:31,380 line number are printed so we know where to look. 16 00:01:31,470 --> 00:01:39,630 In case the input came from a script so we will put a call in after the if statement here and then execute 17 00:01:39,630 --> 00:01:40,330 this line. 18 00:01:41,040 --> 00:01:44,500 So there are no syntax errors in this line. 19 00:01:44,500 --> 00:01:52,530 All right next even if a statement or an expression is syntactically correct it may cause a Nader when 20 00:01:52,530 --> 00:02:02,250 an attempt is made to executed and errors that occur while executing a program are known as exceptions 21 00:02:02,640 --> 00:02:10,140 and we will see examples of different types of exceptions built in exceptions raised while executing 22 00:02:10,570 --> 00:02:11,760 our code. 23 00:02:11,790 --> 00:02:20,070 Both types of these errors these syntax errors and exceptions hide the execution of the program as soon 24 00:02:20,070 --> 00:02:21,150 as they're encountered. 25 00:02:22,190 --> 00:02:26,970 So now let's go and see some examples of exceptions next. 26 00:02:27,050 --> 00:02:36,080 In this example here I'm trying to divide the number two by zero and when we tried to execute this line 27 00:02:36,110 --> 00:02:44,750 the Python interpreter has thrown this exception and in the last line of the error message indicate 28 00:02:44,840 --> 00:02:48,350 what happened says Zito. 29 00:02:48,380 --> 00:02:57,600 Division error and the rest of the line provide details based on the type of the exception and what 30 00:02:57,600 --> 00:02:58,390 caused it. 31 00:02:58,530 --> 00:02:59,010 So. 32 00:02:59,010 --> 00:03:02,460 Division by zero is not allowed. 33 00:03:02,460 --> 00:03:05,690 So let's change this denominator. 34 00:03:05,730 --> 00:03:15,380 Say we will give 2 divided by 7 and then execute so here we have a floating point number. 35 00:03:15,380 --> 00:03:17,630 Now let's look at another example. 36 00:03:18,020 --> 00:03:26,810 Let us know divide the number 2 with debatable X and now the Python interpreter has raised an exception 37 00:03:27,230 --> 00:03:38,440 and this exception is name error and the last line of the error message displays what kind of exception 38 00:03:38,500 --> 00:03:40,090 is encountered. 39 00:03:40,120 --> 00:03:50,160 So name error is the exception and in the details we see that the name X or the variable x is not defined. 40 00:03:50,170 --> 00:03:58,090 So nowhere in our program we have defined the variable X and we have started using this variable in 41 00:03:58,090 --> 00:04:04,400 our operations and it is not allowed let's look at another example. 42 00:04:04,420 --> 00:04:10,120 We will not concatenate a string with a number and see if this is possible. 43 00:04:11,560 --> 00:04:13,700 And then execute. 44 00:04:13,700 --> 00:04:18,050 Now the Python interpreter has thrown an exception. 45 00:04:18,050 --> 00:04:20,510 The exception is a tight budget. 46 00:04:20,510 --> 00:04:26,850 That's because we are trying to concatenate a string but an integer which is not possible. 47 00:04:26,900 --> 00:04:34,920 Now let let's define a list list one contains the numbers 1 2 and 3. 48 00:04:34,920 --> 00:04:39,390 Now let's try accessing these elements in the list. 49 00:04:39,390 --> 00:04:45,030 List of 0 gives me the first element. 50 00:04:45,030 --> 00:04:51,660 Let's try to access element number four element add position 4. 51 00:04:51,950 --> 00:04:58,150 So as you can see there are only three elements that we have defined in the list. 52 00:04:58,190 --> 00:04:59,230 List 1. 53 00:04:59,300 --> 00:05:06,860 But when we are trying to access the elements in the list we have given the index number as for and 54 00:05:06,860 --> 00:05:08,030 there is no number. 55 00:05:08,030 --> 00:05:16,190 With this index in the list and hence the interpreter has raised this exception Index a list index out 56 00:05:16,190 --> 00:05:18,050 of range. 57 00:05:18,210 --> 00:05:31,380 Next we will try to import a script using the import statement and then execute this line and the interpreter 58 00:05:31,410 --> 00:05:39,190 has thrown this exception module not phone we have not created any module with this name test. 59 00:05:39,210 --> 00:05:48,270 So these are all the different errors that have occurred while executing the program and these errors 60 00:05:48,330 --> 00:05:50,390 are called exceptions. 61 00:05:50,550 --> 00:06:00,060 And when this happens we say Python has raised an exception or an exception is thrown whenever such 62 00:06:00,150 --> 00:06:02,710 errors or exceptions happen. 63 00:06:02,730 --> 00:06:11,400 Python creates a special type of object which contains all the relevant information about the error 64 00:06:11,880 --> 00:06:13,320 that has occurred. 65 00:06:13,350 --> 00:06:17,540 For example it contains the line number at which the edit occurred. 66 00:06:17,550 --> 00:06:28,070 The error messages and so on and by default the exceptions hard the execution of the program. 67 00:06:28,260 --> 00:06:38,490 The last line of the error message indicates what happened and the rest of the line provides details 68 00:06:38,490 --> 00:06:46,740 based on the type of exception and what caused it and the preceding part of the error message here shows 69 00:06:46,740 --> 00:06:52,800 the context where the exception happened in the form of a stack trace back. 70 00:06:52,830 --> 00:07:02,040 So when our program detects a fund that will keep it from completing as expected or will generate erroneous 71 00:07:02,040 --> 00:07:10,260 results we should hold for the execution and report the error by throwing an exception and bringing 72 00:07:10,260 --> 00:07:19,020 an exception to attention as call raising the exception and if the exception is left unattended the 73 00:07:19,020 --> 00:07:26,760 default behavior is for the interpreter to print a full trace back an error message included in the 74 00:07:26,760 --> 00:07:29,460 exception for some script. 75 00:07:29,460 --> 00:07:37,860 This behavior is sufficient but it is nice to catch the exception and print a more user friendly version 76 00:07:37,950 --> 00:07:45,210 of the error so we can decide how our programs respond to different types of errors. 77 00:07:45,270 --> 00:07:52,850 We may want to prompt the user for more input or display extended a road or a warning message.