本文介绍了捕获异常后python cmd模块返回提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这段代码,我正在使用python 3:
I have this code and I'm using python 3:
import cmd
class myShell(cmd.Cmd):
def do_bad(self, arg):
raise Exception('something bad happened')
if __name__ == '__main__':
sh = myShell()
sh.cmdloop()
,我想返回引发异常后转到shell提示符。
and I want to return to the shell-prompt after the exception was thrown. How to do that?
推荐答案
从代码中,从Cmd.onecmd(甚至在循环中)调用函数。
From the code, the functions are called from Cmd.onecmd (even in loop).
您可以简单地覆盖它:
def onecmd(self, line):
try:
return super().onecmd(line)
except:
# display error message
return False # don't stop
优点是您不必停止命令循环。
The advantage is that you don't stop the command loop.
这篇关于捕获异常后python cmd模块返回提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!