COMP 204 Exceptions (continued) and Sets Mathieu Blanchette based on material from Yue Li, Carlos Oliver Gonzalez and Christopher Cameron 1 / 19
Traceback (exceptions can be caused by user input) 1 def BMI( weight , h e i g h t ) : p r i n t ( ”Computing BMI” ) 2 bmi = weight / ( h e i g h t h e i g h t ) 3 ∗ p r i n t ( ”Done computing BMI” ) 4 r e t u r n bmi 5 6 7 def get BMI from user ( ) : w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 8 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 9 bmi = BMI(w, h ) 10 r e t u r n bmi 11 12 13 myBMI = get BMI from user ( ) 14 # Output : 15 # Please e n t e r weight 4 16 # Please e n t e r h e i g h t 0 17 # Computing BMI 18 # Traceback ( most r e c e n t c a l l l a s t ) : 19 # F i l e ” excTraceBack . py ” , l i n e 13 , i n < module > 20 # myBMI = get BMI from user ( ) 21 # F i l e ” excTraceBack . py ” , l i n e 10 , i n < module > 22 # bmi = BMI(w, h ) 23 # F i l e ” excTraceBack . py ” , l i n e 3 , i n < module > 24 # r e t u r n weight / ( h e i g h t h e i g h t ) ∗ 25 # b u i l t i n s . Z e r o D i v i s i o n E r r o r : d i v i s i o n by zero 2 / 19
When Exceptions is not handled ◮ If a function generates an Exception but does not handle it, the Exception is send back to the calling block. ◮ If the calling block does not handle the exception, the Exception is sent back to its calling block... etc. ◮ If no-one handles the Exception, the program terminates and reports the Exception. function function call call BMI(w,h) bmi = weight/(height*height) get_BMI_from_user() ZeroDivisionError exception ZeroDivisionError exception 3 / 19
Handling Exceptions: try and except A program can provide code to handle an Exception, so that it doesn’t crash when one happens. ◮ To be able to handle an exception generated by a piece of code, that code needs to be within a try block. ◮ If the code inside the try block raises an exception, its execution stops and the interpreter looks for code to handle the Exception. ◮ Code for handling Exception is in the except block. try: 1 # do something that may cause an Exception 2 # some more code 3 except <SomeExceptionType>: 4 # do something to handle the Exception 5 # rest of code 6 If L2 raises an Exception of type SomExceptionType, we jump to L4, without executing L3 If L2 doesn’t cause an exception, L3 is executed, and L4 and 5 are not executed. 4 / 19 In both cases, the program continues executing with L6.
BMI function handles the Exceptions it caused. 1 def BMI( weight , h e i g h t ) : p r i n t ( ”Computing BMI” ) 2 t r y : 3 bmi = weight / ( h e i g h t h e i g h t ) 4 ∗ p r i n t ( ”Done computing BMI” ) 5 except Z e r o D i v i s i o n E r r o r : 6 p r i n t ( ” There was a d i v i s i o n by zero ” ) 7 bmi = − 1 # a s p e c i a l code to i n d i c a t e an e r r o r 8 r e t u r n bmi 9 10 11 def get BMI from user ( ) : w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 12 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 13 bmi = BMI(w, h ) 14 p r i n t ( ”Thank you ! ” ) 15 r e t u r n bmi 16 17 18 myBMI = get BMI from user ( ) 19 # Please e n t e r weight 4 20 # Please e n t e r h e i g h t 0 21 # Computing BMI 22 # There was a d i v i s i o n by zero 23 # Thank you ! 5 / 19
BMI function does not handle the Exceptions is causes. get BMI from user handles the Exception raised in BMI function. 1 def BMI( weight , h e i g h t ) : p r i n t ( ”Computing BMI” ) 2 bmi = weight / ( h e i g h t h e i g h t ) 3 ∗ p r i n t ( ”Done computing BMI” ) 4 r e t u r n bmi 5 6 7 def get BMI from user ( ) : w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 8 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 9 t r y : 10 bmi = BMI(w, h ) 11 p r i n t ( ”Thank you ! ” ) 12 except : 13 p r i n t ( ” There was a problem computing BMI” ) 14 bmi= − 1 15 r e t u r n bmi 16 17 18 myBMI = get BMI from user ( ) 19 # Please e n t e r weight 4 20 # Please e n t e r h e i g h t 0 21 # Computing BMI 22 # There was a problem computing BMI 6 / 19
Raising our own Exceptions ◮ Exceptions come from raise statements. ◮ Syntax: raise [exception object] ◮ You can choose to raise any exception object. Obviously a descriptive exception is preferred. def my_divide(a, b): 1 if b == 0: 2 raise ZeroDivisionError 3 else: 4 return a / b 5 7 / 19
We can raise an informative exception 1 # This BMI f u n c t i o n r a i s e s a ValueError Exception 2 # i f the weight or h e i g h t are < = 0 3 def BMI( weight , h e i g h t ) : i f weight < =0 or h e i g h t < = 0 : 4 r a i s e ValueError ( ”BMI handles only p o s i t i v e v a l u e s ” ) 5 p r i n t ( ”Computing BMI” ) 6 r e t u r n weight / ( h e i g h t h e i g h t ) 7 ∗ 8 9 def get BMI from user ( ) : w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 10 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 11 bmi = BMI(w, h ) 12 p r i n t ( ”Thank you ! ” ) 13 r e t u r n bmi 14 15 16 myBMI = get BMI from user ( ) 17 18 # Traceback ( most r e c e n t c a l l l a s t ) : 19 # F i l e ” excTraceBack . py ” , l i n e 16 , i n < module > 20 # myFunction ( ) 21 # F i l e ” excTraceBack . py ” , l i n e 12 , i n < module > 22 # r = r a t i o (5 ,0) 23 # F i l e ” excTraceBack . py ” , l i n e 5 , i n < module > 24 # r a i s e ValueError (”BMI handles only p o s i t i v e v a l u e s ”) 25 # b u i l t i n s . ValueError : BMI handles only p o s i t i v e v a l u e s 8 / 19
Handling exceptions raised from one function in another 1 # This BMI f u n c t i o n r a i s e s a ValueError Exception 2 # i f the weight or h e i g h t are < = 0 3 def BMI( weight , h e i g h t ) : i f weight < =0 or h e i g h t < = 0 : 4 r a i s e ValueError ( ”BMI handles only p o s i t i v e v a l u e s ” ) 5 p r i n t ( ”Computing BMI” ) 6 r e t u r n weight / ( h e i g h t h e i g h t ) 7 ∗ 8 9 def get BMI from user ( ) : w h i l e True : # keep a s k i n g u n t i l v a l i d e n t r y i s obtained 10 w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 11 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 12 t r y : 13 bmi = BMI(w, h ) 14 p r i n t ( ”Thank you ! ” ) 15 break # stop asking , break out of the loop 16 except ValueError : 17 p r i n t ( ” E r r o r c a l c u l a t i n g BMI” ) 18 19 r e t u r n bmi 20 21 22 myBMI = get BMI from user ( ) 9 / 19
How to handle invalid user inputs by try ... except ◮ What if user enters a string that cannot be converted to an integer? (e.g. ”Twelve”) ◮ This would cause a ValueError Exception within the int() function. ◮ To be more robust, our program should catch that Exception and deal with it properly. 10 / 19
1 def BMI( weight , h e i g h t ) : i f weight < =0 or h e i g h t < = 0 : 2 r a i s e ValueError ( ”BMI handles only p o s i t i v e v a l u e s ” ) 3 p r i n t ( ”Computing BMI” ) 4 r e t u r n weight / ( h e i g h t h e i g h t ) 5 ∗ 6 7 def get BMI from user ( ) : w h i l e True : # keep a s k i n g u n t i l v a l i d e n t r y i s obtained 8 t r y : 9 w = i n t ( i n p u t ( ” Please e n t e r weight ” ) ) 10 h = i n t ( i n p u t ( ” Please e n t e r h e i g h t ” ) ) 11 except ValueError : # e x c e p t i o n r a i s e d from i n t () 12 p r i n t ( ” Please only e n t e r i n t e g e r s ” ) 13 e l s e : 14 t r y : 15 bmi = BMI(w, h ) 16 p r i n t ( ”Thank you ! ” ) 17 break # stop asking , break out of the loop 18 except ValueError : # excepion r a i s e d from BMI( ) 19 p r i n t ( ” E r r o r c a l c u l a t i n g BMI” ) 20 r e t u r n bmi 21 22 23 myBMI = get BMI from user ( ) Note: Use else block after a try/catch executes only if the try does not cause an exception. 11 / 19
Okay one last thing: assert ◮ The assert statement is a shortcut to raising exceptions. ◮ Sometimes you don’t want to execute the rest of your code unless some condition is true. def divide(a, b): 1 assert b != 0 2 return a / b 3 ◮ If the assert evaluates to False then an AssertionError exception is raised. ◮ Pro: quick and easy to write ◮ Con: exception error may not be so informative. ◮ Used mostly for debugging and internal checks than for user friendliness. 12 / 19
Sets October 18, 2019 13 / 19
Sets: the unordered container for unique things A set is a compound type (like Lists, Tuples, Strings, Dictionaries) ◮ Stores an unordered set of objects (no indexing possible) ◮ No duplicates ◮ Can contain only immutable objects A Set offers a limited version of the functionality of a List, which enables it to perform its operations faster. 14 / 19
Recommend
More recommend