A
A
apiwi2020-09-04 16:28:36
Python
apiwi, 2020-09-04 16:28:36

How to catch vk_api error?

There is a VK bot in the group, sends messages to users, if a person blocks sending messages, the bot tries to write to him, and throws an error Hello): [901] Can't send messages for users without permission
How can I catch this error, and if it comes out, so that the bot performs some actions.

def write_msg(self, user_id, message):
    try:
      self.vk.method('messages.send', {'user_id': user_id, 'message': message, 'keyboard': keyboard, 'random_id': random.randint(0, 100000000)})
    except Exception as ex:
      print("error (write_msg, {0}, {1}):".format(user_id, message), ex)


The question is probably simple, but I read about exception handling, and did not understand how to catch this one, I hope for help. Thanks

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Valery Zhmyshenko, 2020-09-04
@mcborrrov

I personally share control prints, and then google the problem based on them

S
sergeyfilippov4, 2020-09-06
@sergeyfilippov4

import traceback

try:
    действия ()
except:
    print("error", traceback.fornat_exc() )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question