freeCodeCamp/guide/chinese/python/exception-and-error-handling/index.md

956 B
Raw Blame History

title localeTitle
Exceptions and Errors Handling 异常和错误处理

异常和错误处理

在创建程序时,我们可能会犯错,最终会出现错误,而最糟糕的程序会停止运行, 如果我们在代码中找不到错误或错误,那会更烦人。 简单来说,错误是程序员在编写程序时避免的。 要在python中解决这个问题我们可以使用tryexcept

例:

>>> try: 
 >>> . . . print "this is not a string "+1 
 >>> except: 
 >>> . . . print "error" 
 error 

如果您想从代码中更详细地获取错误消息,可以添加except Exception as err参数except Exception as err

>>> try: 
 >>> . . . print "this is not a string "+1 
 >>> except Exception as err: 
 >>> . . . print "error:\n"+str(err) 
 error: 
 cannot concatenate 'str' and 'int' objects 

更多信息:

错误和例外文档