introduction to programming ibbm101/fall18/... · lecture overview •debugging •exception...

88
Lecture #08 – Testing and Debugging Aykut Erdem, Fuat Akal & Aydın Kaya // Fall 2018 BBM 101 Introduction to Programming I The Adventures of Sherlock Holmes (1939)

Upload: others

Post on 04-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Lecture#08– TestingandDebugging

AykutErdem,Fuat Akal&AydınKaya//Fall2018

BBM101IntroductiontoProgrammingI

TheAdventuresofSherlockHolmes(1939)

Page 2: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Lasttime… Sorting,ListComprehension,Visualization

Datavisualization

Sortingprint("hamlet:", hamlet)

print("sorted(hamlet):", sorted(hamlet))print("hamlet:", hamlet)

print("hamlet.sort():", hamlet.sort())print("hamlet:", hamlet)

M = MonthAverages('Ithaca') bar(range(12),M,facecolor='magenta') xlim(-.2,12)ylabel('Average Hours of Sunlight') title(A.City,fontsize=16)show()

2

[i*2 for i in range(3)]

Listcomprehension

Page 3: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

LectureOverview• Debugging• ExceptionHandling• Testing

3

Disclaimer:Muchofthematerialandslidesforthislecturewereborrowedfrom—R.Anderson,M.ErnstandB.HoweinUniversityofWashingtonCSE140

Page 4: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

LectureOverview• Debugging• ExceptionHandling• Testing

4https://www.reddit.com/r/ProgrammerHumor/comments/1r0cw7/the_5_stages_of_debugging/

Page 5: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TheProblem

Whatyouwantyourprogramtodo Whatyourprogramdoes

Notthesame!

5

Thereisabug!

Page 6: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisDebugging?• GraceHopperwasoneofU.S.’sfirstprogrammers.• ShefoundamothintheMarkIcomputer,whichwascausingerrors,andcalleditacomputer“bug”

• Thus,theworddebuggingiscoinedJ

6

Page 7: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DebuggingTools• Pythonerrormessage• assert• print• Pythoninterpreter• PythonTutor(http://pythontutor.com)• Pythondebugger• Besttool:

7

Page 8: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

1. Thescientificmethod2. Divideandconquer

Ifyoumasterthose,youwillfinddebuggingeasy,andpossiblyenjoyable;-)

8

TwoKeyIdeas

Page 9: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TheScientificMethod

1. Createahypothesis

2. Designanexperimenttotestthathypothesis– Ensurethatityieldsinsight

3. Understandtheresultofyourexperiment– Ifyoudon’tunderstand,thenpossiblysuspendyourmainlineofworktounderstandthat

9

Page 10: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TheScientificMethod

Tips:

• Besystematic– Neverdoanythingifyoudon'thaveareason– Don’tjustflail

• Randomguessingislikelytodigyouintoadeeperhole

• Don’tmakeassumptions(verifythem)

10

Page 11: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

ExampleExperiments1. Analternateimplementationofafunction– Runallyourtestcasesafterward

2. Anew,simplertestcase– Examples:smallerinput,ortestafunctioninisolation

– Canhelpyouunderstandthereasonforafailure

11

Page 12: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

YourScientificNotebookRecordeverythingyoudo• Specificinputsandoutputs(bothexpectedandactual)• Specificversionsoftheprogram

– Ifyougetstuck,youcanreturntosomethingthatworks– Youcanwritemultipleimplementationsofafunction

• Whatyouhavealreadytried• Whatyouareinthemiddleofdoingnow

– Thismaylooklikeastack!

• Whatyouaresureof,andwhy

Yournotebookalsohelpsifyouneedtogethelporreproduceyourresults.

12

Page 13: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

ReadtheErrorMessage

Traceback (most recent call last):File "nx_error.py", line 41, in <module>print(friends_of_friends(rj, myval))

File "nx_error.py", line 30, in friends_of_friendsf = friends(graph, user)

File "nx_error.py", line 25, in friendsreturn set(graph.neighbors(user))#

File "/Library/Frameworks/…/graph.py", line 978, in neighborsreturn list(self.adj[n])

TypeError: unhashable type: 'list'

Listofallexceptions(errors):http://docs.python.org/3/library/exceptions.html#bltin-exceptionsTwootherresources,withmoredetailsaboutafewoftheerrors:http://inventwithpython.com/appendixd.htmlhttp://www.cs.arizona.edu/people/mccann/errors-python

Callstackortraceback

Firstfunctionthatwascalled(<module> meanstheinterpreter)

Secondfunctionthatwascalled

Lastfunctionthatwascalled(thisonesufferedanerror)

Theerrormessage:dauntingbutuseful.Youneedtounderstand:• theliteralmeaningof

theerror• theunderlying

problemscertainerrorstendtosuggest13

Page 14: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

CommonErrorTypes

• AssertionError– Raisedwhenanassertstatementfails.

• IndexError– Raisedwhenasequencesubscriptisoutofrange.

• KeyError– Raisedwhenamapping(dictionary)keyisnotfoundinthesetofexistingkeys.

• KeyboardInterrupt– Raisedwhentheuserhitstheinterruptkey(normallyControl-CorDelete).

14

Page 15: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

CommonErrorTypes

• NameError– Raisedwhenalocalorglobalnameisnotfound.

• SyntaxError– Raisedwhentheparserencountersasyntaxerror.

• IndentationError– Baseclassforsyntaxerrorsrelatedtoincorrectindentation.

• TypeError– Raisedwhenanoperationorfunctionisappliedtoanobjectofinappropriatetype.

15

Page 16: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DivideandConquer

• Whereisthedefect(or“bug”)?• Yourgoalistofindtheoneplacethatitis• Findingadefectisoftenharderthanfixingit

• Initially,thedefectmightbeanywhereinyourprogram– Itisimpracticaltofinditifyouhavetolookeverywhere

• Idea:bitbybitreducethescopeofyoursearch• Eventually,thedefectislocalizedtoafewlinesoroneline– Thenyoucanunderstandandfixit

16

Page 17: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DivideandConquer

• 4waystodivideandconquer:– Intheprogramcode– Intestcases– Duringtheprogramexecution– Duringthedevelopmenthistory

17

Page 18: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

• Localizethedefecttopartoftheprogram– e.g.,onefunction,oronepartofafunction

• Codethatisn’texecutedcannotcontainthedefect

18

DivideandConquerintheProgramCode

Page 19: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Threeapproaches:1. Testonefunctionatatime

19

DivideandConquerintheProgramCode

Page 20: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Threeapproaches:2. Addassertionsorprintstatements– Thedefectisexecutedbeforethefailingassertion(andmaybeafterasucceedingassertion)

20

DivideandConquerintheProgramCode

Page 21: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Threeapproaches:3. Splitcomplexexpressionsintosimplerones

Example:Failureinresult = set({graph.neighbors(user)})

Changeittonbors = graph.neighbors(user)

nbors_set = {nbors}

result = set(nbors_set)

Theerroroccursonthe“nbors_set ={nbors}"line21

DivideandConquerintheProgramCode

Page 22: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DivideandConquerinTestCases

• Yourprogramfailswhenrunonsomelargeinput– It’shardtocomprehendtheerrormessage– Thelogofprintstatementoutputisoverwhelming

• Tryasmallerinput– Chooseaninputwithsomebutnotallcharacteristicsofthelargeinput

– Example:duplicates,zeroesindata,…

22

Page 23: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DivideandConquerinExecutionTimeviaPrint(or“logging”)Statements

• Asequenceofprint statementsisarecordoftheexecutionofyourprogram

• Theprint statementsletyouseeandsearchmultiplemomentsintime

• Printstatementsareausefultechnique,inmoderation• Bedisciplined

– Toomuchoutputisoverwhelmingratherthaninformative– Rememberthescientificmethod:haveareason(ahypothesistobetested)foreachprintstatement

– Don’tonly useprintstatements

23

Page 24: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

DivideandConquerinDevelopmentHistory• Thecodeusedtowork(forsometestcase)• Thecodenowfails• Thedefectisrelatedtosomelineyouchanged

• Thisisusefulonlyifyoukeptaversionofthecodethatworked(usegoodnames!)

• Thisismostusefulifyouhavemadefewchanges• Moral:testoften!– Fewerlinestocompare– Yourememberwhatyouwerethinking/doingrecently

24

Page 25: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

AMetaphorAboutDebugging

Ifyourcodedoesn’tworkasexpected,thenbydefinitionyoudon’tunderstandwhatisgoingon.

• You’relostinthewoods.• You’rebehindenemylines.• Allbetsareoff.• Don’ttrustanyoneoranything.

Don’tpressonintounexploredterritory-- gobackthewayyoucame! (andleavebreadcrumbs!)

You’retryingto“advancethefrontlines,”not“trailblaze”25

Page 26: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

• Thegameistogofrom“workingtoworking”• Whensomethingdoesn’twork,STOP!

– It’swildoutthere!• FIRST:Gobacktothelastsituationthatworkedproperly.

– Rollbackyourrecentchangesandverifythateverythingstillworksasexpected.

– Don’tmakeassumptions– bydefinition,youdon’tunderstandthecodewhensomethinggoeswrong,soyoucan’ttrustyourassumptions.

– Youmayfindthatevenwhatpreviouslyworkednowdoesn’t– Perhapsyouforgottoconsidersome“innocent”orunintentional

change,andnoweventestedcodeisbroken

26

Time-SavingTrick:MakeSureYouareDebuggingtheRightProblem

Page 27: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

ABadTimeline

• Aworks,socelebratealittle• NowtryB• Bdoesn’twork• ChangeBandtryagain• ChangeBandtryagain• ChangeBandtryagain…

27

https://xkcd.com/1739/

Page 28: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

ABadTimeline

• Aworks,socelebratealittle• NowtryB• Bdoesn’twork• ChangeBandtryagain• ChangeBandtryagain• ChangeBandtryagain…

28

fromgiphy.com

Page 29: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

ABetterTimeline• Aworks,socelebratealittle• NowtryB• Bdoesn’twork• RollbacktoA• DoesAstillwork?

– Yes:FindA’thatissomewherebetweenAandB– No:Youhaveunintentionallychangedsomethingelse,andthere’sno

pointfutzingwithBatall!

These“innocent”andunnoticedchangeshappenmorethanyouwouldthink!• Youaddacomment,andtheindentationchanges.• Youaddaprintstatement,andafunctionisevaluatedtwice.• Youmoveafile,andthewrongoneisbeingread• Youareonadifferentcomputer,andthelibraryisadifferentversion

29

Page 30: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

OnceYouareonSolidGroundYoucanSetOutAgain• Onceyouhavesomethingthatworksandsomethingthatdoesn’twork,itisonlyamatteroftime

• Youjustneedtoincrementallychangetheworkingcodeintothenon-workingcode,andtheproblemwillrevealitself.

• Variation:Perhapsyourcodeworkswithoneinput,butfailswithanother.Incrementallychangethegoodinputintothebadinputtoexposetheproblem.

30

Page 31: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

SimpleDebuggingToolsprint

– showswhatishappeningwhetherthereisaproblemornot

– doesnotstopexecutionassert

– Raisesanexceptionifsomeconditionisnotmet– Doesnothingifeverythingworks– Example:assert len(rj.edges()) == 16

– Usethisliberally!Notjustfordebugging!

31

Page 32: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

32

Page 33: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

33

Page 34: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

LectureOverview• Debugging• ExceptionHandling• Testing

34

Page 35: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisanException?• Anexceptionisanabnormalcondition(andthusrare)thatarisesinacodesequenceatruntime.

• Forinstance:– Dividinganumberbyzero– Accessinganelementthatisoutofboundsofanarray– Attemptingtoopenafilewhichdoesnotexist

35

Page 36: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisanException?• Whenanexceptionalconditionarises,anobjectrepresentingthatexceptioniscreatedandthrowninthecodethatcausedtheerror

• Anexceptioncanbecaughttohandleitorpassiton

• Exceptionscanbegeneratedbytherun-timesystem,ortheycanbemanuallygeneratedbyyourcode

36

Page 37: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisanException?test = [1,2,3]test[3]

37

IndexError: list index out of range

Page 38: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisanException?successFailureRatio = numSuccesses/numFailures

print('The success/failure ratio is', successFailureRatio)

print('Now here')

38

ZeroDivisionError: integer division or modulo by zero

Page 39: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisanException?val = int(input('Enter an integer: '))

print('The square of the number', val**2)

> Enter an integer: asd

39

ValueError: invalid literal for int() with base 10: 'asd'

Page 40: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

HandlingExceptions• Exception mechanism gives the programmer achanceto dosomething against anabnormal condition.

• Exception handling isperforming anaction inresponseto anexception.

• This action may be:– Exiting the program– Retrying the action with or without alternative data– Displaying anerror message and warning user to dosomething

– ....

40

Page 41: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

HandlingExceptionstry:

successFailureRatio = numSuccesses/numFailures

print('The S/F ratio is', successFailureRatio)

except ZeroDivisionError:

print('No failures, so the S/F is undefined.')

print('Now here')

41

• Uponenteringthetry block,theinterpreterattemptstoevaluatetheexpressionnumSuccesses/numFailures.

• Ifexpressionevaluationissuccessful,theassignmentisdoneandtheresultisprinted.

• If,however,aZeroDivisionError exceptionisraised,theprintstatementintheexcept blockisexecuted.

Page 42: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

while True: val = input('Enter an integer: ') try:

val = int(val) print('The square of the number', val**2) break #to exit the while loop

except ValueError: print(val, 'is not an integer')

42

HandlingExceptions

ChecksforwhetherValueError exceptionisraisedornot

Page 43: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

KeywordsofExceptionHandling• There are five keywords inPython to deal withexceptions:try,except,else,raise and finally.

• try:Creates ablock to monitor if any exceptionoccurs.

• except:Follows the try block and catches anyexception which isthrown within it.

43

Page 44: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

AreThereManyExceptionsinPython?

• Yes,someofthemare…– Exception– ArithmeticError– OverflowError– ZeroDivisonError– EOFError– NameError– IOError– SyntaxError

44

Listofallexceptions(errors):http://docs.python.org/3/library/exceptions.html#bltin-exceptions

Page 45: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

MultipleexceptStatements

45

• It ispossible that more than one exception canbethrown inacode block.– Wecanusemultipleexcept clauses

• Whenanexceptionisthrown,eachexceptstatementisinspectedinorder,andthefirstonewhosetypematches thatoftheexceptionisexecuted.– Type matching means that the exception thrown must beanobject ofthe same class or asub-class ofthe declared classinthe except statement

• Afteroneexcept statementexecutes,theothersarebypassed.

Page 46: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

MultipleexceptStatements

46

try:

Youdoyouroperationshere;except Exception-1:

Executethisblock.except Exception-2:

Executethisblock.except (Exception-3[, Exception-4[,...ExceptionN]]]):

Ifthereisanyexceptionfromthegivenexceptionlist,thenexecutethisblock.

except (ValueError, TypeError):…

Theexceptblockwillbeenteredifanyofthelistedexceptions israisedwithinthetryblock

Page 47: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

MultipleexceptStatementstry:

f = open('outfile.dat', 'w')dividend = 5divisor = 0division = dividend / divisorf.write(str(division))

except IOError:print("I can't open the file!")

except ZeroDivisionError:print("You can't divide by zero!")

47

Youcan'tdividebyzero!

Page 48: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

MultipleexceptStatementstry:

f = open('outfile.dat', 'w')dividend = 5divisor = 0division = dividend / divisorf.write(str(division))

except Exception:print("Exception occured and handled!")

except IOError:print("I can't open the file!")

except ZeroDivisionError:print("You can't divide by zero!")

48

Exceptionoccured andhandled!

Page 49: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

MultipleexceptStatementstry:

f = open('outfile.dat', 'w')dividend = 5divisor = 0division = dividend / divisorf.write(str(division))

except:print("Exception occured and handled!")

except IOError:print("I can't open the file!")

except ZeroDivisionError:print("You can't divide by zero!")

49

SyntaxError: default 'except:' must be last

Page 50: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

except-elseStatements

50

try:You do your operations here

except: Execute this block.

else: If there is no exception, execute this block.

try:f = open(arg, 'r')

except IOError:print('cannot open', arg)

else:print(arg, 'has', len(f.readlines()), 'lines')

Page 51: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

finally Statement

51

• finally creates ablock ofcode that will beexecuted afteratry/except block hascompleted and before the codefollowing the try/except block

• finally block isexecuted whether or notexception isthrown

• finally block isexecuted whether or notexception iscaught

• It isused to guarantee that acode block will beexecuted inanycondition.

Page 52: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

finally Statement

52

Youcanuseittocleanupfiles,databaseconnections,etc.

try:You do your operations here

except: Execute this block.

finally: This block will definitely be executed.

try: file = open('out.txt', 'w') do something…

finally: file.close()os.path.remove('out.txt')

Page 53: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Nestedtry Blocks• When anexception occurs insideatry block;

– If the try block does nothave amatching except,then the outertry statement’s except clauses are inspected for amatch

– If amatching except isfound,that except block isexecuted– If no matching except exists,execution flow continues to find a

matching except by inspecting the outer try statements– If amatching except cannot befound atall,the exception will be

caught by Python’s exception handler.

• Execution flow never returns to the line that exceptionwas thrown.This means,anexception iscaught andexcept block isexecuted,the flow will continue with thelines following this except block

53

Page 54: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Let’s clarify itonvarious scenarios

54

try:statement1try:

statement2except Exception1:

statement3except Exception2:

statement4;try:

statement5except Exception3:

statement6statement7;

except Exception3:statement8

statement9;

Information: Exception1andException2aresubclassesofException3

Question:Whichstatementsareexecutedif1- statement1throwsException12- statement2throwsException13- statement2throwsException34- statement2throwsException1andstatement3throws Exception2

Page 55: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Scenario:statement1throws Exception1

55

try:statement1try:

statement2except Exception1:

statement3except Exception2:

statement4;try:

statement5except Exception3:

statement6statement7;

except Exception3:statement8

statement9;

Exception1Step1:Exceptionisthrown

Step2:except clauses ofthetryblockareinspectedforamatching except statement.Exception3issuperclassofException1,soitmatches.

Step3:statement8isexecuted,exceptionishandledandexecutionflowwillcontinuebypassingthefollowing except clauses

Step4:statement9isexecuted

Page 56: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Scenario:statement2throws Exception1

56

try:statement1try:

statement2except Exception1:

statement3except Exception2:

statement4;try:

statement5except Exception3:

statement6statement7;

except Exception3:statement8

statement9;

Exception1Step1:Exceptionisthrown

Step2:except clauses ofthetryblockareinspectedforamatching except statement.Firstclausecatchestheexception

Step3:statement3isexecuted,exceptionishandled

Step4:executionflowwillcontinuebypassingthefollowing except clauses.statement5isexecuted.

Step5:Assumingnoexceptionisthrownbystatement5,programcontinueswithstatement7andstatement9.

Page 57: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Scenario:statement2throws Exception3

57

try:statement1try:

statement2except Exception1:

statement3except Exception2:

statement4;try:

statement5except Exception3:

statement6statement7;

except Exception3:statement8

statement9;

Exception3Step1:Exceptionisthrown

Step2:except clauses ofthetryblockareinspectedforamatching except statement.Noneofthese except clauses matchException3

Step3:except clauses oftheoutertrystatementareinspectedforamatching except .Exception3iscatchedandstatement8isexecuted

Step4:statement9isexecuted

Page 58: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Scenario:statement2throws Exception1and statement3throws Exception2

58

try:statement1try:

statement2except Exception1:

statement3except Exception2:

statement4;try:

statement5except Exception3:

statement6statement7;

except Exception3:statement8

statement9;

Exception1Step1:Exceptionisthrown

Step2:Exceptioniscatchedandstatement3isexecuted.

Step3:statement3throwsanewexception

Step5:statement9isexecuted

Exception2

Step4:Except clauses oftheoutertrystatementareinspectedforamatching except.Exception2iscatchedandstatement8isexecuted

Page 59: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

raise Statement• Youcanraiseexceptionsbyusingtheraisestatement.

• Thesyntaxisasfollows:raise exceptionName(arguments)

59

Page 60: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

raise Statementdef getRatios(vect1, vect2):

ratios = []for index in range(len(vect1)):

try:ratios.append(vect1[index]/vect2[index])

except ZeroDivisionError:ratios.append(float('nan')) #nan = Not a Number

except:raise ValueError(’getRatios called with bad arguments’)

return ratios

try:print(getRatios([1.0, 2.0, 7.0, 6.0], [1.0,2.0,0.0,3.0]))print(getRatios([], []))print(getRatios([1.0, 2.0], [3.0]))

except ValueError as msg:print(msg)

60

[1.0, 1.0, nan, 2.0][]getRatios called with bad arguments

Page 61: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

raise Statement• AvoidraisingagenericException! Tocatchit,you'llhave

tocatchallothermorespecificexceptionsthatsubclassit.

61

def demo_bad_catch(): try:

raise ValueError('a hidden bug, do not catch this')raise Exception('This is the exception you expect to handle')

except Exception as error:print('caught this error: ' + repr(error))

>>> demo_bad_catch()caught this error: ValueError('a hidden bug, do not catch this',)

Page 62: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

raise Statement• andmorespecificcatcheswon'tcatchthegeneralexception:..

62

def demo_no_catch(): try:

raise Exception('general exceptions not caught by specific handling')except ValueError as e:

print('we will not catch e')

>>> demo_no_catch()Traceback (most recent call last):

File "<stdin>", line 1, in <module> File "<stdin>", line 3, in demo_no_catch

Exception: general exceptions not caught by specific handling

Page 63: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

CustomExceptions

63

• UserscandefinetheirownexceptionbycreatinganewclassinPython.

• Thisexceptionclasshastobederived,eitherdirectlyorindirectly,fromtheExceptionclass.

• Mostofthebuilt-inexceptionsarealsoderivedformthisclass.

Page 64: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

CustomExceptions

class ValueTooSmallError(Exception):"""Raised when the input value is too small"""pass

class ValueTooLargeError(Exception):"""Raised when the input value is too large"""pass

64

Page 65: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

CustomExceptions

number = 10 # you need to guess this number

while True:try:

i_num = int(input("Enter a number: "))if i_num < number:

raise ValueTooSmallErrorelif i_num > number:

raise ValueTooLargeErrorbreak

except ValueTooSmallError:print("This value is too small, try again!")

except ValueTooLargeError:print("This value is too large, try again!")

print("Congratulations! You guessed it correctly.")

65

Page 66: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

LectureOverview• Debugging• ExceptionHandling• Testing

66

Page 67: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Testing• Programmingtoanalyzedataispowerful• Itisuselessiftheresultsarenotcorrect• Correctnessisfarmoreimportantthanspeed

67

Page 68: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

FamousExamples• Ariane 5rocket– OnJune4,1996,themaidenflightoftheEuropeanAriane5launchercrashedabout40secondsaftertakeoff.

– Mediareportsindicatedthattheamountlostwashalfabilliondollars

– Theexplosionwastheresultofasoftwareerror

• Therac-25radiationtherapymachine– In1985aCanadian-builtradiation-treatmentdevicebeganblastingholesthroughpatients'bodies.

68

Page 69: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TestingdoesnotProve Correctness• Edsger Dijkstra:“Programtestingcanbeusedtoshowthepresenceofbugs,butnevertoshowtheirabsence!”

69

Page 70: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Testing=Double-CheckingResults• Howdoyouknowyourprogramisright?– Compareitsoutputtoacorrectoutput

• Howdoyouknowacorrectoutput?– Realdataisbig– Youwroteacomputerprogrambecauseitisnotconvenienttocomputeitbyhand

• Usesmallinputssoyoucancomputebyhand

• Example:standarddeviation– Whataregoodtestsforstd_dev?

70

Page 71: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Testing≠Debugging• Testing:Determiningwhether yourprogramiscorrect– Doesn’tsaywhere orhow yourprogramisincorrect

• Debugging:Locatingthespecificdefectinyourprogram,andfixingit2keyideas:– divideandconquer– thescientificmethod

71

Page 72: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisaTest?

• Atestconsistsof:– aninput:sometimescalled“testdata”– anoracle:apredicate(boolean expression)oftheoutput

72

Page 73: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhatisaTest?

• Exampletestforsum:– input:[1,2,3]– oracle:resultis6– writethetestas:sum([1, 2, 3]) == 6

• Exampletestforsqrt:– input:3.14– oracle:resultiswithin0.00001of1.772– waystowritethetest:• -0.00001 < sqrt(3.14) – 1.772 < 0.00001• math.abs(sqrt(3.14) – 1.772) < 0.00001

73

Page 74: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TestResults• Thetestpasses ifthebooleanexpressionevaluatestoTrue

• Thetestfails ifthebooleanexpressionevaluatestoFalse

• Usetheassert statement:– assert sum([1, 2, 3]) == 6– assert True doesnothing– assert False crashestheprogramandprintsamessage

74

Page 75: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WheretoWriteTestCases• Atthetoplevel:isruneverytimeyouloadyourprogram

def hypotenuse(a, b):…

assert hypotenuse(3, 4) == 5assert hypotenuse(5, 12) == 13

• Inatestfunction:isrunwhenyouinvokethefunctiondef hypotenuse(a, b):

…def test_hypotenuse():

assert hypotenuse(3, 4) == 5assert hypotenuse(5, 12) == 13

75

Page 76: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

AssertionsarenotJustforTestCases• Useassertionsthroughoutyourcode

• Documentswhatyouthinkistrueaboutyouralgorithm

• Letsyouknowimmediatelywhensomethinggoeswrong– Thelongerbetweenacodemistakeandtheprogrammernoticing,theharderitistodebug

76

Page 77: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

AssertionsMakeDebuggingEasier• Common,butunfortunate,courseofevents:

– Codecontainsamistake(incorrectassumptionoralgorithm)– Intermediatevalue(e.g.,resultofafunctioncall)isincorrect– Thatvalueisusedinothercomputations,orcopiedintoothervariables

– Eventually,theusernoticesthattheoverallprogramproducesawrongresult

– Whereisthemistakeintheprogram?Itcouldbeanywhere.

• Supposeyouhad10assertionsevenlydistributedinyourcode– Whenonefails,youcanlocalizethemistaketo1/10ofyourcode(thepartbetweenthelastassertionthatpassesandthefirstonethatfails)

77

Page 78: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WheretoWriteAssertions• Functionentry:Areargumentslegal?

– Placeblameonthecallerbeforethefunctionfails

• Functionexit:Isresultcorrect?

• Placeswithtrickyorinterestingcode

• Assertionsareordinarystatements;e.g.,canappearwithinaloop:

for n in myNumbers:assert type(n) == int or type(n) == float

78

Page 79: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

Wherenot toWriteAssertions• Don’tclutterthecode

– Sameruleasforcomments

• Don’twriteassertionsthatarecertaintosucceed– Theexistenceofanassertiontellsaprogrammerthatitmightpossiblyfail

• Don’twriteanassertionifthefollowingcodewouldfailinformatively

assert type(name) == strprint("Hello, " + name)

• Writeassertionswheretheymaybeusefulfordebugging

79

Page 80: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhattoWriteAssertionsAbout• Resultsofcomputations

• Correctly-formeddatastructures

assert 0 <= index < len(mylist)assert len(list1) == len(list2)

80

Page 81: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhentoWriteTests• Twopossibilities:–Writecodefirst,thenwritetests–Writetestsfirst,thenwritecode

81

Page 82: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhentoWriteTests• Ifyouwritethecodefirst,youremembertheimplementationwhilewritingthetests– Youarelikelytomakethesamemistakesintheimplementation

82

Page 83: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WhentoWriteTests• Ifyouwritethetestsfirst,youwillthinkmoreaboutthefunctionalitythanaboutaparticularimplementation– Youmightnoticesomeaspectofbehaviorthatyouwouldhavemadeamistakeabout

– Thisisthebetterchoice

83

Page 84: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

WritetheWholeTest• Acommonmistake:

1. Writethefunction2. Makeuptestinputs3. Runthefunction4. Usetheresultastheoracle

• Youdidn’twriteatest,butonlyhalfofatest– Createdthetestsinputs,butnottheoracle

• Thetestdoesnotdeterminewhetherthefunctioniscorrect– Onlydeterminesthatitcontinuestobeascorrect(orincorrect)asitwasbefore

84

Page 85: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TestingApproaches• Blackboxtesting- Choosetestdatawithoutlookingatimplementation

• Glassbox(whitebox,clearbox)testing -Choosetestdatawith knowledgeofimplementation

85

Page 86: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

InsideKnowledgemightbeNice• Assumethecodebelow:

c = a + bif c > 100

print("Tested”)print("Passed”)

• Creatingatestcasewitha=40andb=70isnotenough– Althougheverylineofthecodewillbeexecuted

• Anothertestcasewitha=40andb=30wouldcompletethetest

86

Page 87: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

TestsmightnotRevealanErrorSometimes

def mean(numbers):"""Returns the average of the argument list.

The argument must be a non-empty number list."""return sum(numbers)//len(numbers)

# Testsassert mean([1, 2, 3, 4, 5]) == 3assert mean([1, 2, 3]) == 2

Thisimplementationiselegant,butwrong!

mean([1,2,3,4]) à should return 2.5!!!

87

Page 88: Introduction to Programming Ibbm101/fall18/... · Lecture Overview •Debugging •Exception Handling •Testing 3 Disclaimer:Much of the material and slides for this lecture were

LastbutnotLeast,Don’tWriteMeaninglessTestsdef mean(numbers):"""Returns the average of the argument list.

The argument must be a non-empty number list."""return sum(numbers)//len(numbers)

Unnecessarytests.Don’twritethese:

mean([1, 2, "hello"])mean("hello")mean([])

88