Python Forum
Error handling using cmd module
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error handling using cmd module
#1
I'm using Python's cmd module to write my own command line interpreter. I'm not sure how to handle errors using the modules.

Let's say my command eat requires 1 argument.

The function usually goes as follows:

    def do_eat(self, arg):
        args = arg.split()
        if len(args) < 1:
            print("Missing arguments!")
            return
However, I was wondering if one could write it as:

    def do_eat(self, arg):
        args = arg.split()
        if len(args) < 1:
            raise "Missing arguments" # Or raise an enum
Reply
#2
I suggest
print("Error: missing arguments.")
It will print an error message, but will not exit the intrepreter as an exception would. Also the word "Error" must appear so that the user knows immediatly that something is going wrong.
Reply
#3
It's the question of responsibility. If you raise an Error, you must catch it on the caller side, but the caller is in this case the loop of cmd. Without changing code, you can't raise an Exception and catch it outside the function.

By the way, I was asking myself how to stop the command loop:

import cmd


class Cmd(cmd.Cmd):
    def do_quit(self, agrg):
        return True


Cmd().cmdloop()
If you enter quit, the method do_quit is called and True is returned to the postcmd method and stops the command loop.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Here is my solution:

Every command has the following template:

    def do_eat(self, arg):
        try:
            # eat
        except Exception as exc:
            return exc
Then I print the errors:

    def postcmd(self, exc, line):
        if exc != True:
            print("Error: {}".format(exc))
Later on, when I unittest:

        self.assertEqual(self.cmd_class.do_eat("args"), None) # if success
        self.assertIsInstance(self.cmd_class.do_eat("args"), ExceptionClass) # if fail
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star python exception handling handling .... with traceback mg24 3 1,263 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,490 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 5,874 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 2,928 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Excpetion Handling Getting Error Number gw1500se 4 2,361 May-29-2020, 03:07 PM
Last Post: gw1500se
  vlc module error pythonprogrammer 1 2,864 Apr-23-2020, 04:16 AM
Last Post: Larz60+
  Handling exception from a module dchi2 11 5,583 Nov-25-2019, 08:47 AM
Last Post: dchi2
  Warning / Error handling in python Prarthana_12 1 5,095 Feb-08-2019, 09:21 PM
Last Post: snippsat
  Help With Error Handling jo15765 6 4,095 Sep-14-2018, 06:27 PM
Last Post: jo15765
  Error Handling/No results from SQL Query JP_ROMANO 7 9,414 Jul-18-2018, 02:31 PM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020