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()
and I want to return to the shell-prompt after the exception was thrown. How to do that?
From the code, the functions are called from Cmd.onecmd (even in loop).
You can simply override it:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With