PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN...
Transcript of PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN...
![Page 1: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/1.jpg)
PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON
Madhavan Mukund, Chennai Mathematical Institute http://www.cmi.ac.in/~madhavan
Week 5, Lecture 1
NPTEL MOOC
![Page 2: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/2.jpg)
When things go wrong
y = x/z, but z has value 0
y = int(s), but string s is not a valid integer
y = 5*x, but x does not have a value
y = l[i], but i is not a valid index for list l
Try to read from a file, but the file does not exist
Try to write to a file, but the disk is full
![Page 3: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/3.jpg)
When things go wrong …
Some errors can be anticipated
Others are unexpected
Predictable error — exception
Normal situation vs exceptional situation
Contingency plan — exception handling
![Page 4: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/4.jpg)
Exception handlingIf something goes wrong, provide “corrective action”
File not found — display a message and ask user to retype filename
List index out of bounds — provide diagnostic information to help debug error
Need mechanism to internally trap exceptions
An untapped exception will abort the program
![Page 5: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/5.jpg)
Types of errors
Python notifies you of different types of errors
Most common error, invalid Python code
SyntaxError: invalid syntax
Not much you can do with this!
We are interested in errors that occur when code is being executed
![Page 6: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/6.jpg)
Types of errors
Some errors while code is executing (run-time errors)
Name used before value is definedNameError: name 'x' is not defined
Division by zero in arithmetic expressionZeroDivisionError: division by zero
Invalid list indexIndexError: list assignment index out of range
![Page 7: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/7.jpg)
TerminologyRaise an exception
Run time error → signal error type, with diagnostic informationNameError: name 'x' is not defined
Handle an exception
Anticipate and take corrective action based on error type
Unhandled exception aborts execution
![Page 8: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/8.jpg)
Handling exceptions
try: . . . . . . except IndexError: . . . except (NameError,KeyError): . . . except: . . . else: . . .
⟵ Code where error may occur
⟵ What to do if IndexError occurs
⟵ Common code to handle multiple errors
⟵ Catch all other exceptions
⟵ Execute if try terminates normally, no errors
![Page 9: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/9.jpg)
“Positive” use of exceptions
Add a new entry to this dictionary
scores = {'Dhawan':[3,22],'Kohli':[200,3]}
Batsman b already exists, append to list
scores[b].append(s)
New batsman, create fresh entry
scores[b] = [s]
![Page 10: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/10.jpg)
Traditional approach
if b in scores.keys(): scores[b].append(s) else: scores[b] = [s]
“Positive” use of exceptions
Using exceptions
try: scores[b].append(s) except KeyError: scores[b] = [s]
![Page 11: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/11.jpg)
Flow of control.. x = f(y,z)
![Page 12: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/12.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
![Page 13: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/13.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
![Page 14: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/14.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..
![Page 15: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/15.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..IndexError, not handled in h( )⟶
![Page 16: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/16.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..IndexError, not handled in h( )⟶
IndexError inherited from h( )⟶
![Page 17: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/17.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..IndexError, not handled in h( )⟶
IndexError inherited from h( )⟶Not handled?
⟵IndexError inherited from g( )
![Page 18: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/18.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..IndexError, not handled in h( )⟶
IndexError inherited from h( )⟶Not handled?
⟵IndexError inherited from g( )Not handled?
IndexErrorinherited from f( )
![Page 19: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/19.jpg)
Flow of control.. x = f(y,z)
def f(a,b): .. g(a)
def g(m): .. h(m)
def h(s): .. ..IndexError, not handled in h( )⟶
IndexError inherited from h( )⟶Not handled?
⟵IndexError inherited from g( )Not handled?
IndexErrorinherited from f( )Not handled?Abort!
![Page 20: PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHONmadhavan/nptel-python-2016/week5/pdf/python-wee… · PROGRAMMING, DATA STRUCTURES AND ALGORITHMS IN PYTHON Madhavan Mukund, Chennai](https://reader033.fdocuments.us/reader033/viewer/2022052406/5aa695617f8b9ab4788ea0cf/html5/thumbnails/20.jpg)
Summary
Exception handling allows us to gracefully deal with run time errors
Can check type of error and take appropriate action based on type
Can change coding style to exploit exception handling
When dealing with files and input/output, exception handling becomes very important