Python-3.4.3/C4/Handling-Errors-and-Exceptions/English-timed
From Script | Spoken-Tutorial
Revision as of 17:26, 10 June 2019 by PoojaMoolya (Talk | contribs)
Time | Narration |
00:01 | Welcome to the spoken tutorial on Handling errors and exceptions. |
00:07 | In this tutorial, you will learn to, Understand errors and exceptions |
00:14 | Handle errors and exceptions |
00:17 | To record this tutorial, I am using Ubuntu Linux 16.04 operating system |
00:25 | Python 3.4.3 and IPython 5.1.0 |
00:32 | To practise this tutorial, you should know how to use functions and carry-out testing and debugging. |
00:43 | If not, see the relevant Python tutorials on this website. |
00:48 | In Python there are two kinds of errors: syntax errors and exceptions |
00:57 | Syntax errors are caused by incorrect usages and these are detected by parser. |
01:04 | For example: if True print inside brackets inside double quotes done |
01:11 | It will give Syntax Error since colon is missing after True. |
01:17 | Exception is an error that occurs during execution of a program. |
01:23 | Python generates an exception that can be handled, which avoids the program to crash. |
01:30 | For example: 1/0
It will give ZeroDivisionError exception. |
01:37 | Let us see these examples in action. Open the terminal. |
01:43 | Type ipython3 and press Enter. |
01:48 | From here onwards remember to press the Enter key after typing every command on the terminal. |
01:55 | Now type if True print inside brackets inside double quotes done. |
02:03 | The output shows SyntaxError. |
02:06 | It displays an ‘arrow’ pointing at the earliest point in the line where the error was detected. |
02:13 | In our case, the error is caused by missing colon after True in If clause |
02:20 | Now type, 1 / 0
Python throws an exception called ZeroDivisionError. |
02:29 | Even though the expression is syntactically correct, we cannot divide a number by zero. |
02:36 | Exception is a special kind of failure reported by the programming language. |
02:42 | Let us see how can be deal with the Exceptions that occured in Programs |
02:48 | Type a = input inside brackets inside double quotes Enter an integer |
02:57 | I will enter ac as input. Now type, num is equal to int inside brackets a |
03:07 | When you run this code, it throws a 'ValueError' Exception. This is because, we are trying to convert the string to integer. |
03:17 | So now we can 'catch exceptions and write code to handle it. For this we have try and except clause in Python. |
03:27 | Here is the syntax of try....except...else blocks. First, the statements between the try and except keywords is executed. |
03:38 | If the statements do not cause any exception, the except clause is skipped. |
03:44 | If the statements cause any exception, then except clause is executed if exception name matches. |
03:52 | And the execution continues after the try statement |
03:57 | The code in the else-block executes if the code in the try: block does not raise an exception. |
04:05 | Type as shown. Give ac as input. |
04:12 | Now we will type the try and except blocks. |
04:16 | Press Enter twice to get the output. |
04:20 | In the previous example, We encountered a problem because we tried to convert the string ‘ac’ to integer. |
04:28 | Here conversion of string value to an integer is given inside try block. |
04:34 | ValueError exception is raised and so the output is displayed as Wrong input. |
04:41 | In the previous example, we found out what caused the error and then resolved to get a solution for it. This whole process is called debugging. |
04:53 | Next we will see another case in try except statement with else clause. |
04:59 | Lets change our previous code slightly. Type as shown.
I will give input as 23. |
05:10 | Now type as shown. This try… except statement has an optional else clause. |
05:20 | It is useful if the try clause does not raise an exception. |
05:25 | Lets see another example for debugging. Create a file mymodule.py with the following code. |
05:34 | The variable ‘i’ is iterated from 0 to 9. |
05:39 | Let us run this code in Ipython.
Type, from mymodule import test test open and close brackets |
05:52 | Interpreter gives us ZeroDivisionError because there is a division by zero error. |
05:59 | To find the value which caused the error, type Percentage debug |
06:06 | The code which caused the error is shown by an arrow. |
06:11 | The prompt has changed to ipdb which is the ipython debugger mode. |
06:18 | Using this debugger here, you can access variables in the previous code block. |
06:24 | We can check values of variables to inspect what went wrong. |
06:29 | For example, on typing ‘i’, we get 5 as output. |
06:36 | This means that the error was caused when the value of i became 5. |
06:42 | To exit from the ipdb prompt, press q and press Enter. |
06:48 | This brings us to the end of this tutorial. Let us summarize. |
06:54 | In this tutorial, we have learnt about Errors and exceptions. |
07:00 | Handling exception using try and except. Using percentage debug for debugging in ipython. |
07:09 | Here is a self assessment question for you to solve
How do you start the debugger on ipython? |
07:17 | And the answer, We start the debugger on ipython by saying, percentage debug |
07:25 | Please post your timed queries in this forum. |
07:29 | Please post your general queries on Python in this forum. |
07:34 | FOSSEE team coordinates the TBC project. |
07:38 | Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website. |
07:49 | This is Priya from IIT Bombay signing off. Thank you. |