

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1 Exception Handling 1 1.1 A function That Raises an Error with Try Statement Catching it................ 1 1.2 Catch an Exception Noisily..................................... 1 1.3 Handle Parameters When Conditions Not Satisfied........................ 2 1.4 Proceed Despite Error........................................ 3
Go to the RMD , PDF , or HTML version of this file. Go back to Python Code Examples Repository (bookdown site) or the pyfan Package (API).
Below, we have a function that will raise a TypeError unless we provide an integer input. The function is called with integer input and then called with a string input. The string input is wrapped in a try and except call, where the exception catches the TypeError and prints it.
# define the function def ffi_error_test(gn_speckey = None): if isinstance(gn_speckey, int): print(f'{gn_speckey = } is an integer') else : raise TypeError (f'{gn_speckey = } is not an integer') # Call function with integer error_test(1) # Call function with string
try : ffi_error_test('abc') except TypeError as error: print('Caught this error: ' + repr(error))
Rather than only catching the last element of the exception, show the full trace. Notice in the example below, the second element of the loop does not work as function input. With exception catching, the loop continued despite the second element not working. The traceback shows more details at the end with full trace of the exception from the second element of the list as input for ffi_error_test().
import traceback import numpy as np
ls_ob_inputs = [2, ['abc','efg'], 1, 123]
for ob_input in ls_ob_inputs: print(f'try input {ob_input = } with the error_test function:') try : ffi_error_test(ob_input) except TypeError as error: traceback.print_exc() print('Caught this error: ' + repr(error))
see How to print the stack trace of an exception object in Python?
There is a function, that can estimate or simulate, under both functionalities, there is a common string parameter, that requires specifying estimation or simulation conditions. The common string parameter should be a simple string without special separators in the case of simulation, and should be four strings concatenated together with equal sign for estimation. Generate an exception if the function is called for estimation but the string parameter does not have the required structure.
_# ls_st_spec_key_dict = ['NG_S_D', 'NG_S_D=KAP_M0_NLD_M_SIMU=2=3']
if estimate and not isinstance(spec_key_dict, str): pass elif (estimate is False and isinstance(spec_key_dict, str)) or (estimate is False and isinstance(spec_ke pass else : st_error = 'speckey=' + speckey + ' and estimate=' + str(estimate)