主要是做个笔记,以后可以方便梳理一下,尊重版权,特此申明下面信息均来自公司培训课PPT Nagiza F. Samatova, NC State Univ. All rights
实践
n = input('Nominator: ') d = input('Denominator: ') try: devision = float(n) / float(d) except ZeroDivisionError as e: print ('Error handle-Exception: {} \n Type: {} \ \n Tuple of Messages: {} ' .format(e, type(e), e.args)) else: print('Perform_if_try_is_successfully') finally: print ('Always perform action: Trying to divide…')no exception, perform else->finally
# output: Nominator: 6 Denominator: 2 Perform_if_try_is_successfully Always perform action: Trying to divide…exception type catch, perform exception->finally e.args: Extract additional messages about the error
# output: Nominator: 6 Denominator: 0 Error handle-Exception: float division by zero Type: <class 'ZeroDivisionError'> \ Tuple of Messages: ('float division by zero',) Always perform action: Trying to divide…exception occured, but does not match the exception type, perform finally->throw exception
# output: Nominator: aa Denominator: 3 Always perform action: Trying to divide… --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-282-3eb1b8a9856e> in <module> 2 d = input('Denominator: ') 3 try: ----> 4 devision = float(n) / float(d) 5 except ZeroDivisionError as e: 6 print ('Error handle-Exception: {} \n Type: {} \ \n Tuple of Messages: {} ' .format(e, type(e), e.args)) ValueError: could not convert string to float: 'aa'Grouping exception handling
n = input('Nominator: ') d = input('Denominator: ') try: devision = float(n) / float(d) except (ValueError, ZeroDivisionError) as e: print ('Error handle-Exception: {} \n Type: {} \ \n Tuple of Messages: {} ' .format(e, type(e), e.args)) else: print('Perform_if_try_is_successfully') finally: print ('Always perform action: Trying to divide…')Exception: Generic Exception Handling Avoid too generic exception types: - be as specific as possible (check Hierarchy)
n = input('Nominator: ') d = input('Denominator: ') try: devision = float(n) / float(d) except Exception as e: print ('Error handle-Exception: {} \n Type: {} \ \n Tuple of Messages: {} ' .format(e, type(e), e.args)) else: print('Perform_if_try_is_successfully') finally: print ('Always perform action: Trying to divide…')raise statement is used to explicitly trigger either built-in exceptions or user-defined exception
def divide_operation(nominator, denominator): if denominator == 0: raise ZeroDivisionError ("Which grade are you in") else: return float(nominator) / float(denominator) print(divide_operation(5, 2)) divide_operation(5, 0) # output: 2.5 --------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) <ipython-input-291-7c2394d04a90> in <module> 6 7 print(divide_operation(5, 2)) ----> 8 divide_operation(5, 0) <ipython-input-291-7c2394d04a90> in divide_operation(nominator, denominator) 1 def divide_operation(nominator, denominator): 2 if denominator == 0: ----> 3 raise ZeroDivisionError ("Which grade are you in") 4 else: 5 return float(nominator) / float(denominator) ZeroDivisionError: Which grade are you in作业: