# Author: James Tam # Version: November 17 2010 # An example to illustrate error handling using exceptions vs. using # a standard if construct. For simple examples like this you won't really # find a major difference between the two approaches. (Fun2 shows a bit # of information about the function calls if you enter a negative value). # If you take CPSC 233 you will see cases where handling an error using an # exception is much better than using a series of if's. # Error checks using a standard branching construct def fun1 (age): if (age < 0): print "age can't be negative" # Error checks by creating an exception def fun2 (age): if (age < 0): raise ValueError, "age can't be negative" def main (): age = input ("What's your age? ") fun1 (age) age = input ("What's your age? ") fun2 (age) main ()