0

I am quite new to programming so that currently struggling to make telegram bot where I can send pre-defined template message to my receivers.

In the course of my work, I encountered 'error message' thrown like below when it tries to send messages to those whose 'entity' like user_name does not exist, wrongly spelled or whatever.

ERROR:root:Cannot find any entity corresponding to "CHANNELS2RSS BOT"

To just skip those entities, I wrote following codes --

except ValueError as e:
    error_msg = str(e)
    if "Cannot find any entity" in error_msg:
        logging.error(error_msg + " >> skipping this entity")
        break

Is this way of handling exception good? If there is another solution, please tell me..

jin
  • 3
  • 2
  • No. Error *messages* are usually meant for humans, not machines, and may change without warning. Ideally, you should inspect only proper attributes of exceptions. Of course, if the message is all you have then there is no other option... – MisterMiyagi Nov 15 '18 at 11:41
  • 2
    for code reviewing there is a special [StackExchange site](https://codereview.stackexchange.com) – Azat Ibrakov Nov 15 '18 at 11:47

1 Answers1

0

For what you want you should create a custom error is a class that derive from Exception see Proper way to declare custom exceptions in modern Python?. and catch it ex:

class CannotFindEntity(Exception):
    pass

class Finder():
    def find(iterator):
        # do something
        if something:
            raise CannotFindEntity  # raise the error
        else:
            return  # or raise other Error

then you can throw this error and catch it when needed. But that only works if you writting the code that launch the exception.

RomainL.
  • 997
  • 1
  • 10
  • 24