Aradh S
Aradh S

Reputation: 23

Exception bypassing in Looping call Python

import sys
from twisted.internet import reactor, defer, task
from twisted.python import log
def periodic_task():
    log.msg("periodic task running")
    x = 10 / 0

def periodic_task_crashed(reason):
    log.err(reason, "periodic_task broken")

log.startLogging(sys.stdout)

my_task = task.LoopingCall(periodic_task)
d = my_task.start(1)
d.addErrback(periodic_task_crashed)
reactor.run()

I am getting the output and it stops the script. is ther any way to continue run the script even if there is an exception error . to be frank instead of x = 10 / 0 I am doing some api calls . but when there is an error it stops the script. But what I want is to run the script even if there is an error and check again and again.

Upvotes: 1

Views: 78

Answers (1)

Raydel Miranda
Raydel Miranda

Reputation: 14360

Just handle the exeption, use a try ... except block around the code you know might fail.

def periodic_task():
    log.msg("periodic task running")
    try:
        x = 10 / 0
    except Exception as error:
        # Here you should at least log the error, exceptions do not should pass silently.
        pass

Upvotes: 2

Related Questions