一种选择可能是:def sum_of_max(*args): if not all(type(x) in (float, int) for x in args): raise TypeError("Parameters should be of type 'int' or 'float'") my_list = list(args) first_max = max(my_list) my_list.remove(max(my_list)) return first_max + max(my_list)print(sum_of_max( "qwerty", 1, 6))# Output: TypeError: Parameters should be of type 'int' or 'float'但它只是max()用更明确的异常替换了引发的异常。最好的选择可能是保持函数原样,并在使用它时捕获潜在的错误:try: print(sum_of_max("qwerty", 1, 6))except ValueError: # Do what you want in case of ValueError