Python-3.4.3/C4/Handling-Errors-and-Exceptions/English
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) |
Lets 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 except exception name: exception handling statement(s) else: statement(s) when no exception
|
Here is the syntax of try....except...else blocks.
And the execution continues after the try statement.
|
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 this
|
[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
1. How do you start the debugger on ipython? |
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. |