'Python: How to Handle the Exception to Class variable

I have no idea to implement to handle the class variable exception. I am trying to create python module which has dependency with mongodb.

class HrmLookUps(DBMixin):
    db_handle = DBMixin.get_db_handle(DBMixin, MONGO_STORE_DICT.get("DB_NAME"),
                                      MONGO_STORE_DICT.get("DB_HOST"),
                                      MONGO_STORE_DICT.get("DB_PORT"),
                                      MONGO_STORE_DICT.get("USERNAME"),
                                      MONGO_STORE_DICT.get("PASSWORD"))

    @classmethod
    def get_gender(cls):
        collection_name = COLLECTION_NAME_DICT.get("COLLECTION_GENDER")
        # collection_name = "departments"
        gender_record = cls.get_from_db(cls, cls.db_handle, collection_name)
        if gender_record[collection_name]:
            return gender_record
        else:
            raise KeyError("Collection Name Invalid!")

I have multiple get method like above get_gender(). I am handling each method raised Keyerror if its data empty. My question is,

  1. Is this proper way to handle exception of methods?
  2. If my class variable has some issues ex.database credential wrong how can I handle that?


Solution 1:[1]

Currently your else clause isn't really doing anything. Your if statement would already throw an error if collection_name wasn't a key in gender_record. So unless you want to raise KeyError in the else clause when collection_name is in gender_record but its value happens to be 0 or False or None.

I think what you are trying to do is something closer to these examples:

    @classmethod
    def get_gender(cls):
        collection_name = COLLECTION_NAME_DICT.get("COLLECTION_GENDER")
        # collection_name = "departments"
        gender_record = cls.get_from_db(cls, cls.db_handle, collection_name)
        if collection_name in gender_record:
            return gender_record
        else:
            raise KeyError("Collection Name Invalid!")

or this maybe

    @classmethod
    def get_gender(cls):
        collection_name = COLLECTION_NAME_DICT.get("COLLECTION_GENDER")
        # collection_name = "departments"
        gender_record = cls.get_from_db(cls, cls.db_handle, collection_name)
        try:
            gender_record[collection_name]
            return gender_record
        else KeyError as err:
            raise KeyError("Collection Name Invalid!") from err

Handling exceptions is pretty much the same in any situation. so for the credentials it would be something like this:


try:
    db_handle = DBMixin.get_db_handle(DBMixin, MONGO_STORE_DICT.get("DB_NAME"),
                                      MONGO_STORE_DICT.get("DB_HOST"),
                                      MONGO_STORE_DICT.get("DB_PORT"),
                                      MONGO_STORE_DICT.get("USERNAME"),
                                      MONGO_STORE_DICT.get("PASSWORD"))
except <SomeCredentialErrorHere>:
   # whatever you put here is what gets executed if the error is raised.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexpdev