Python 编程中的异常处理与面向对象编程应用
1. 异常处理相关内容
异常处理在编程中十分重要,它是一种强大的方式,可用于传达异常情况或错误条件,而无需调用函数显式检查返回值。Python 中有许多内置异常,引发它们非常容易,并且有多种不同的语法来处理不同的异常事件。
以下是一个示例代码,展示了异常处理的应用:
raise SystemExit()
def menu(self):
try:
answer = ""
while True:
print("""
Please enter a command:
\tlogin\tLogin
\ttest\tTest the program
\tchange\tChange the program
\tquit\tQuit
""")
answer = input("enter a command: ").lower()
try:
func = self.menu_map[answer]
except KeyError:
print("{} is not a valid option".format(
answer))
else:
func()
finally:
print("Thank you for testing the auth module")
Edi