Glassbox testing

Path-complete testing is not necessarily enough

def abs(x):
  """ Assumes x is an int
      Returns x if x>=0 and –x otherwise """
  if x < -1:
    return –x
  else:
    return x

Debugging

Print statements

Debugging steps

Error messages - easy Consider the following program

test = [1, 2, 3]

Logic Errors - Hard

Exceptions and Assertions

Other examples of exceptions: already seen common datatypes

Dealing with exceptions

You can have separate except clauses to deal with a particular type of exception

try:
  a = int(input("Tell me one number:"))
  b = int(input("Tell me another number:"))
  print(a/b)
except ValueError:
  print("Could not convert to a number.")  # only executes if ValueError comes up
except ZeroDivisionError:
  print("Can't divide by zero.")  # only executes if ZeroDivisionError comes up
except:  # for all other errors
  print("Something else went wrong.")

What to do when you encounter an exception?

Exceptions as control flow

Syntax:
raise <exceptionName>(<arguments>)
Example:
raise ValueError("something is wrong")  #The string description is optional