Difference between revisions of "Python-3.4.3/C4/Handling-Errors-and-Exceptions/English"
(Created page with "'''Title of script''': '''Handling errors and exceptions''' '''Author: Arun KP''' '''Keywords: Python, Ipython, error handling, exceptions, debugging, video tutorial''' {|...") |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 24: | Line 24: | ||
* Understand '''errors '''and''' exceptions''' | * Understand '''errors '''and''' exceptions''' | ||
− | * Handle ''' | + | * Handle '''errors''' and '''exceptions''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 37: | Line 35: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 53: | Line 49: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide Errors | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Slide Errors | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In Python there are two kinds of errors: | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In '''Python''' there are two kinds of '''errors''': |
− | * '''''syntax errors'' '''and | + | * '''''syntax errors'''''and |
* '''''exceptions''''' | * '''''exceptions''''' | ||
− | |||
− | |||
|- | |- | ||
Line 64: | Line 58: | ||
Syntax Error | Syntax Error | ||
− | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Syntax errors '''are caused by incorrect usages and these are detected by parser. | + | | style="background-color:#ffffff;border-top:none;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Syntax errors '''are caused by incorrect usages and these are detected by '''parser'''. |
For example: | For example: | ||
− | if True print'' inside brackets inside double quotes ''done | + | '''if True print'' inside brackets inside double quotes ''done''' |
− | It will give '''Syntax Error''' since colon is missing after '''True'''. | + | It will give '''Syntax Error''' since '''colon''' is missing after '''True'''. |
|- | |- | ||
Line 81: | Line 75: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Exception is an error that occurs during execution of a program. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Exception''' is an '''error''' that occurs during '''execution''' of a '''program'''. |
− | Python generates an exception that can be handled, which avoids the program to crash. | + | '''Python''' generates an '''exception''' that can be handled, which avoids the '''program''' to crash. |
Line 108: | Line 102: | ||
− | From here onwards remember to press the Enter key after typing every command. | + | From here onwards remember to press the '''Enter''' key after typing every '''command''' on the '''terminal'''. |
|- | |- | ||
Line 128: | Line 122: | ||
− | It displays an ‘arrow’ pointing at the earliest point in the line where the error was detected. | + | It displays an ‘arrow’ pointing at the earliest point in the line where the '''error''' was detected. |
Line 149: | Line 143: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Exception''' is a special kind of failure reported by the '''programming | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| '''Exception''' is a special kind of failure reported by the '''programming language'''. |
|- | |- | ||
Line 159: | Line 153: | ||
'''num = int(a)''' | '''num = int(a)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let's see how can we deal with '''Exceptions '''that occur in '''programs'''. |
Line 174: | Line 168: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Point to the error | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Point to the error | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| When you run this code, it throws a ''''ValueError | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| When you run this code, it throws a ''''ValueError' Exception'''. |
− | This is because, we are trying to convert the string to integer. | + | This is because, we are trying to convert the '''string''' to '''integer'''. |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| So now we can ''''catch | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| So now we can ''''catch exceptions''' and write '''code''' to '''handle''' it. |
− | For this we have '''try''' and '''except | + | For this we have '''try''' and '''except clause''' in '''Python'''. |
|- | |- | ||
Line 197: | Line 191: | ||
'''statement2''' | '''statement2''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is the syntax of '''''try....except...else'' blocks'''. | ||
+ | |||
+ | First, the '''statements''' between the '''try''' and '''except keywords''' is executed. | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
'''except exception name:''' | '''except exception name:''' | ||
'''exception handling statement(s)''' | '''exception handling statement(s)''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| If the '''statements''' do not cause any '''exception''', the '''except clause''' is skipped. | ||
− | |||
− | ''' | + | If the '''statements''' cause any '''exception''', then '''except clause''' is executed if '''exception''' name matches. |
+ | And the '''execution''' continues after the '''try statement'''. | ||
− | ''' | + | |- |
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"|'''else:''' | ||
+ | '''statement(s) when no exception''' | ||
− | |||
− | + | '''after statement(s)''' | |
− | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| The '''code''' in the '''else-block executes''' if the '''code''' in the '''try: block''' does not raise an '''exception'''. | |
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 253: | Line 240: | ||
− | Give '''ac''' as input. | + | Give '''ac''' as '''input'''. |
− | Now we will type the try and except blocks. | + | Now we will type the '''try''' and '''except blocks.''' |
− | Press Enter twice to get the output. | + | Press '''Enter''' twice to get the '''output'''. |
In the previous example, | In the previous example, | ||
− | We encountered a problem because we tried to convert the | + | We encountered a problem because we tried to convert the '''string ‘ac’''' to '''integer'''. |
− | Here conversion of string value to an integer is given inside '''try block'''. | + | Here conversion of '''string value''' to an '''integer''' is given inside '''try block'''. |
− | '''ValueError''' | + | '''ValueError exception''' is raised and so the '''output''' is displayed as '''Wrong input.''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In the previous example, we found out what caused the error and then resolved to get a solution for it. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| In the previous example, we found out what caused the '''error''' and then resolved to get a solution for it. |
Line 280: | Line 267: | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will see another case in try except statement with else clause. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Next we will see another '''case''' in '''try except statement''' with '''else clause'''. |
|- | |- | ||
Line 303: | Line 290: | ||
Highlight '''No exception ''' | Highlight '''No exception ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Lets change our previous code slightly. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Lets change our previous '''code''' slightly. |
Line 309: | Line 296: | ||
− | I will give input as 23. | + | I will give '''input''' as 23. |
Line 315: | Line 302: | ||
− | This''' try… except''' | + | This''' try… except statement''' has an optional '''else clause'''. |
− | It is useful if the try clause does not raise an exception. | + | It is useful if the '''try clause''' does not raise an '''exception'''. |
|- | |- | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open a text editor and type the following code | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Open a '''text editor''' and type the following '''code'''. |
Line 335: | Line 322: | ||
Save it as file '''mymodule.py''' | Save it as file '''mymodule.py''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Lets see another example for debugging. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Lets see another example for '''debugging'''. |
− | Create a ''' | + | Create a file '''mymodule.py''' with the following '''code'''. |
− | The variable ‘i’ is iterated from 0 to 9. | + | The '''variable ‘i’''' is iterated from 0 to 9. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 355: | Line 339: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us run this code in Ipython. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Let us '''run''' this '''code''' in '''Ipython'''. |
Line 365: | Line 349: | ||
− | '''Interpreter''' gives us | + | '''Interpreter''' gives us '''ZeroDivisionError''' because there is a''' division by zero error'''. |
|- | |- | ||
Line 377: | Line 361: | ||
Highlight ipdb> | Highlight ipdb> | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To find the value which caused the error, type | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| To find the value which caused the '''error''', type |
Line 383: | Line 367: | ||
− | The code which caused the error is shown by an arrow. | + | The '''code''' which caused the '''error''' is shown by an arrow. |
Line 395: | Line 379: | ||
'''5''' | '''5''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Using this '''debugger''' here, you can access variables in the previous code block. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Using this '''debugger''' here, you can access '''variables''' in the previous '''code block'''. |
− | We can check values of | + | We can check values of '''variables''' to inspect what went wrong. |
Line 404: | Line 388: | ||
− | This means that the error was caused when the value of i became 5. | + | This means that the '''error''' was caused when the value of '''i''' became 5. |
− | To exit from the '''ipdb | + | To exit from the '''ipdb prompt''', press '''q '''and press '''Enter'''. |
|- | |- | ||
Line 420: | Line 404: | ||
* '''Errors '''and''' exceptions'''. | * '''Errors '''and''' exceptions'''. | ||
* Handling '''exception''' using '''try''' and '''except'''. | * Handling '''exception''' using '''try''' and '''except'''. | ||
− | * Using '' | + | * Using '''''percentage ''debug''' for '''debugging''' in '''ipython'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 430: | Line 412: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| Here is a self assessment question for you to solve | ||
− | + | #How do you start the''' debugger '''on''' ipython?''' | |
|- | |- | ||
Line 436: | Line 418: | ||
Solutions | Solutions | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer, | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.097cm;padding-right:0.191cm;"| And the answer, |
− | + | ||
− | + | ||
− | + | ||
+ | # We start the '''debugger''' on '''ipython''' by saying, '''percentage ''debug''' | ||
|- | |- |
Latest revision as of 11:44, 25 October 2018
Title of script: Handling errors and exceptions
Author: Arun KP
Keywords: Python, Ipython, error handling, exceptions, debugging, video tutorial
Visual Cue | Narration |
Show Slide title | Welcome to the spoken tutorial on Handling errors and exceptions. |
Show Slide
Objectives
|
In this tutorial, you will learn to,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Prerequisite |
To practise this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Slide Errors | In Python there are two kinds of errors:
|
Show Slide
Syntax Error |
Syntax errors are caused by incorrect usages and these are detected by parser.
if True print inside brackets inside double quotes done
|
Show Slide
Exceptions
|
Exception is an error that occurs during execution of a program.
1/0
|
Open terminal | Let us see these examples in action.
|
Type,
ipython3 |
Type ipython3 and press Enter.
|
Type,
if True print("done")
|
Now type
if True print inside brackets inside double quotes done
|
Type,
1 / 0 |
Now type,
1 / 0
|
Exception is a special kind of failure reported by the programming language. | |
Type,
a = input("Enter a number: ") Enter ac num = int(a) |
Let's see how can we deal with Exceptions that occur in programs.
a = input inside brackets inside double quotes Enter an integer
|
Point to the error | When you run this code, it throws a 'ValueError' Exception.
|
So now we can 'catch exceptions and write code to handle it.
| |
[slide]
try..except..else
statement1 statement2 |
Here is the syntax of try....except...else blocks.
|
except exception name: exception handling statement(s) |
If the statements do not cause any exception, the except clause is skipped.
And the execution continues after the try statement. |
else:
statement(s) when no exception
|
The code in the else-block executes if the code in the try: block does not raise an exception. |
Type,
a = input("Enter an integer: ") ac try: num = int(a) except ValueError: print ("Wrong input") Press Enter twice output: Wrong input |
Type as shown.
Now we will type the try and except blocks.
We encountered a problem because we tried to convert the string ‘ac’ to integer.
|
In the previous example, we found out what caused the error and then resolved to get a solution for it.
| |
Next we will see another case in try except statement with else clause. | |
b = input("Enter an integer: ")
23 try: num = int(b) except ValueError: print ("Wrong input") else: print(“No exception “) Press Enter twice
|
Lets change our previous code slightly.
|
Open a text editor and type the following code.
prod = 1 for i in range(0, 10): prod *= i / (i - 5) print(prod) Save it as file mymodule.py |
Lets see another example for debugging.
|
Type,
from mymodule import test test()
|
Let us run this code in Ipython.
from mymodule import test test open and close brackets
|
Type,
%debug
|
To find the value which caused the error, type
|
[IPython Terminal]
5 |
Using this debugger here, you can access variables in the previous code block.
|
Show Slide
Summary |
This brings us to the end of this tutorial. Let us summarize.
|
Show Slide
Self assessment questions |
Here is a self assessment question for you to solve
|
Show Slide
Solutions |
And the answer,
|
Show Slide Forum | Please post your timed queries in this forum. |
Show Slide Fossee Forum | Please post your general queries on Python in this forum. |
Slide Textbook Companion | FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgment
|
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is Priya from IIT Bombay signing off.
Thank you. |