Custid=input("enter custid)
Validate_custid(Custid)
Print(Custid)
validate_custid模块
From connections import cursor
From customExceptions import invalidcustidException
Def validate_custid(custid):
Cursor.execute("select count(custid) from customer where custid=:custid",{"custid":custid})
For row in cursor:
Count=row[0]
If Count==0:
Raise invalidcustidException
到目前为止它在exception.inow中打印消息我希望我的程序在发生此异常时将custid作为输入。该过程应该迭代,直到用户输入有效的custid。
答案 0 :(得分:1)
你想要尝试除了阻止。
try:
# portion of code that may throw exception
except invalidcuspidError:
# stuff you want to do when exception thrown
答案 1 :(得分:1)
您要做的是称为异常处理。我认为Python文档比我更好地解释了这一点,所以在这里你去:https://docs.python.org/2/tutorial/errors.html#handling-exceptions
答案 2 :(得分:1)
你应该使用带有else语句的try-except块:
while True:
custid = input('Input custom Id: ')
try:
# Put your code that may be throw an exception here
validate_custid(custid)
except InvalidcustidException as err:
# Handle the exception here
print(err.strerror)
continue # start a new loop
else:
# The part of code that will execute when no exceptions thrown
print('Your custom id {} is valid.'.format(custid))
break # escape the while loop
请看这里:https://docs.python.org/3.4/tutorial/errors.html#handling-exceptions