未正确处理异常
Exceptions not being handled correctly
我的异常处理似乎没有正常工作。当我输入一个字符串作为输入时,没有出现正确的异常。当我输入负数时,我遇到了同样的问题。有谁知道如何解决这个问题?
我尝试更改引发异常的条件,但仍然没有找到解决问题的方法。
def main():
try:
height = int(input())
check_height = type(height)
if height == 0 or height > 999:
raise Exception
elif check_height != int:
raise TypeError
elif height < 0:
raise ValueError
else:
for blocks in range(height):
if blocks == 0:
print ("+-+")
print ("| |")
print ("+-+", end = "")
else:
print ("-+")
for num in range(blocks):
print(" ", end = "")
print ("| |")
for num in range(blocks):
print(" ", end = "")
print ("+-+", end = "")
print("\n")
except Exception:
print("Value can't be equal to 0 or greater than 999")
except TypeError:
print("Value is not an integer")
except ValueError:
print("Value is less than 0")
finally:
pass
main()
如果输入的输入是 1,预期的输出应该是一个如下所示的块:(请参见上面的输出屏幕截图)
您的异常处理有问题。由于您正在尝试将输入本身转换为整数,因此如果输入无法转换为整数,它会在此处抛出 ValueError 。而且您没有任何 ValueError 异常处理,因此它会转到默认异常块。试试这个方法:
try:
height = int(input())
# check_height = type(height)
if height <= 0 or height > 999:
raise Exception
# elif check_height != int:
# raise TypeError
# elif height < 0:
# raise ValueError
else:
for blocks in range(height):
if blocks == 0:
print ("+-+")
print ("| |")
print ("+-+", end = "")
else:
print ("-+")
for num in range(blocks):
print(" ", end = "")
print ("| |")
for num in range(blocks):
print(" ", end = "")
print ("+-+", end = "")
print("\n")
except ValueError:
print("Value is not an integer")
# except TypeError:
# print("Value is not an integer")
except Exception:
print("Value can't be less than 1 or greater than 999")
finally:
pass
我的异常处理似乎没有正常工作。当我输入一个字符串作为输入时,没有出现正确的异常。当我输入负数时,我遇到了同样的问题。有谁知道如何解决这个问题?
我尝试更改引发异常的条件,但仍然没有找到解决问题的方法。
def main():
try:
height = int(input())
check_height = type(height)
if height == 0 or height > 999:
raise Exception
elif check_height != int:
raise TypeError
elif height < 0:
raise ValueError
else:
for blocks in range(height):
if blocks == 0:
print ("+-+")
print ("| |")
print ("+-+", end = "")
else:
print ("-+")
for num in range(blocks):
print(" ", end = "")
print ("| |")
for num in range(blocks):
print(" ", end = "")
print ("+-+", end = "")
print("\n")
except Exception:
print("Value can't be equal to 0 or greater than 999")
except TypeError:
print("Value is not an integer")
except ValueError:
print("Value is less than 0")
finally:
pass
main()
如果输入的输入是 1,预期的输出应该是一个如下所示的块:(请参见上面的输出屏幕截图)
您的异常处理有问题。由于您正在尝试将输入本身转换为整数,因此如果输入无法转换为整数,它会在此处抛出 ValueError 。而且您没有任何 ValueError 异常处理,因此它会转到默认异常块。试试这个方法:
try:
height = int(input())
# check_height = type(height)
if height <= 0 or height > 999:
raise Exception
# elif check_height != int:
# raise TypeError
# elif height < 0:
# raise ValueError
else:
for blocks in range(height):
if blocks == 0:
print ("+-+")
print ("| |")
print ("+-+", end = "")
else:
print ("-+")
for num in range(blocks):
print(" ", end = "")
print ("| |")
for num in range(blocks):
print(" ", end = "")
print ("+-+", end = "")
print("\n")
except ValueError:
print("Value is not an integer")
# except TypeError:
# print("Value is not an integer")
except Exception:
print("Value can't be less than 1 or greater than 999")
finally:
pass