未捕获 ValueError
ValueError not being caught
我在捕获 ValueError 异常时遇到了一些问题。
- 如果我用字符串测试输入,即 'a' 而不是提供 int 数字,那么它会按预期抛出 ValueError。但是为什么我的异常没有捕获这个并打印“错误,用整数再试一次”?
我给了它 've' 因为我还没有用过这个变量。我没有给出 'e',因为我已经将其用于另一个例外。
- 如果我改为尝试 except (TypeError,ValueError): 那么为什么这也不起作用?
注意,这段代码只是让我练习处理异常,内容本身没有意义。
while not correct_input:
distance_travelled = float(input("enter distance travelled in km: ")) #input enters strings, so you must convert to the datatype you want
monetary_value = float(input("amount paid to cover distance: "))
price_per_litre = 1.59
try:
fuel_consumed = monetary_value/price_per_litre
if monetary_value/price_per_litre < 0:
raise Exception #I can raise my own custom exception here
except Exception as e:
print("too small, try again")
except ValueError as ve: #Catch our exception, and handle it properly. Ensure specific exceptions at the top, general at the bottom
print("error, try again with an integer")
else: #Runs code if try doesn't raise an exception
print(fuel_consumed)
correct_input = True
finally: #Regardless of error or not, what you wish for it to do. i.e. close a file, close database etc
continue
谢谢
< 0
只有当您输入负值时,您才会满足此条件,请查看您是否需要或 < 1
。否则代码如下。
correct_input = False
while not correct_input:
try: # just check if you are getting integer/floating value here only
distance_travelled = float(input("enter distance travelled in km: ")) #input enters strings, so you must convert to the datatype you want
monetary_value = float(input("amount paid to cover distance: "))
except:
print('Only Integer and Floting point number')
price_per_litre = 1.59
try:
fuel_consumed = monetary_value/price_per_litre
if monetary_value/price_per_litre < 0: # I have a doubt if you really want 0 here. OR 1
raise ValueError # raise value error when condition is met.
except ValueError as ve:
print('Too Small')
else:
print(fuel_consumed)
correct_input = True
finally:
continue
我在捕获 ValueError 异常时遇到了一些问题。
- 如果我用字符串测试输入,即 'a' 而不是提供 int 数字,那么它会按预期抛出 ValueError。但是为什么我的异常没有捕获这个并打印“错误,用整数再试一次”?
我给了它 've' 因为我还没有用过这个变量。我没有给出 'e',因为我已经将其用于另一个例外。
- 如果我改为尝试 except (TypeError,ValueError): 那么为什么这也不起作用?
注意,这段代码只是让我练习处理异常,内容本身没有意义。
while not correct_input:
distance_travelled = float(input("enter distance travelled in km: ")) #input enters strings, so you must convert to the datatype you want
monetary_value = float(input("amount paid to cover distance: "))
price_per_litre = 1.59
try:
fuel_consumed = monetary_value/price_per_litre
if monetary_value/price_per_litre < 0:
raise Exception #I can raise my own custom exception here
except Exception as e:
print("too small, try again")
except ValueError as ve: #Catch our exception, and handle it properly. Ensure specific exceptions at the top, general at the bottom
print("error, try again with an integer")
else: #Runs code if try doesn't raise an exception
print(fuel_consumed)
correct_input = True
finally: #Regardless of error or not, what you wish for it to do. i.e. close a file, close database etc
continue
谢谢
< 0
只有当您输入负值时,您才会满足此条件,请查看您是否需要或 < 1
。否则代码如下。
correct_input = False
while not correct_input:
try: # just check if you are getting integer/floating value here only
distance_travelled = float(input("enter distance travelled in km: ")) #input enters strings, so you must convert to the datatype you want
monetary_value = float(input("amount paid to cover distance: "))
except:
print('Only Integer and Floting point number')
price_per_litre = 1.59
try:
fuel_consumed = monetary_value/price_per_litre
if monetary_value/price_per_litre < 0: # I have a doubt if you really want 0 here. OR 1
raise ValueError # raise value error when condition is met.
except ValueError as ve:
print('Too Small')
else:
print(fuel_consumed)
correct_input = True
finally:
continue