V
V
Vyacheslav Grachunov2019-10-30 23:36:09
Python
Vyacheslav Grachunov, 2019-10-30 23:36:09

How to catch exceptions in asynchronous functions?

For example, a piece of code (new message event in telethon):

@client.on(events.NewMessage(chats=('chat')))
      async def normal_handler(event):
        print(event.message.id)
        posts = await client(functions.messages.GetHistoryRequest(
        peer=event.message.to_id,
        limit=1,
        offset_date=None,
        offset_id=0,
        max_id=0,
        min_id=0,
        add_offset=0,
        hash=0))
        print(posts)

If somewhere below async you make an error or even a syntactically incorrect expression, the program does not crash with an exception, but hangs stupidly and silently. How to fix this? How to catch errors?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question