W
W
Worgen2018-09-24 21:21:37
Python
Worgen, 2018-09-24 21:21:37

How to return back to the code after an error?

Suppose I got
a Traceback error (most recent call last):
File "C:/Users/Worgen/Desktop/bot/test.py", line 17, in
if 138231317 != vk.messages.getLongPollHistory(ts=ts)[ 'messages']['items'][0]["from_id"]:
IndexError: list index out of range
Process finished with exit code 1
How do I return to the code after an error?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vadim Shatalov, 2018-09-24
@Worgen

try:
    if 138231317 != vk.messages.getLongPollHistory(ts=ts)['messages']['items'][0]["from_id"]:
    # код
except IndexError:
    #  обработка этой ошибки
except Exception as ex:
    # обработка других ошибок
finally:
    # учи матчасть

T
timur_sh, 2018-09-24
@timur_sh

You need to wrap this code in a try-catch and catch the IndexError exception in catch and handle it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question