How would i make a custom error message in python
在我编写的python程序中,我希望它只接受整数,如果它得到一个字符串,就说"系统中出现了错误",而不是用户无法理解的无意义信息
使用
1 2 3 4 | try: a = int(input()) except: raise Exception('There has been an error in the system') |
您需要使用
1 2 3 4 5 6 7 8 | try: value = int(input("Enter an integer:")) except ValueError: print("There has been an error in the system.") input() # To let the user see the error message # if you want to then exit the program import sys sys.exit(1) |
如果您不想使用
1 2 3 4 | import sys def my_except_hook(exctype, value, traceback): print('There has been an error in the system') sys.excepthook = my_except_hook |
如果出现错误,则只打印出指定的错误消息。此外,这可以防止显示堆栈跟踪。
进口ctypesctypes.windll.user32。MessageBoxW(None, u"CUSTOM MESSAGE", u"TITLE BAR", 0)