简体   繁体   English

Python,如何在捕获异常后使程序继续运行

[英]Python, how to keep program going after exception is caught

probably a simple question as I fairly new to python and programming in general but I am currently working on improving a program of mine and can't figure out how to keep the program going if an exception is caught. 可能是一个简单的问题,因为我对python和程序设计相当陌生,但是我目前正在改进我的程序,因此无法弄清楚如何在捕获到异常的情况下使程序继续运行。 Maybe I am looking at it the wrong way but for example I have something along these lines: 也许我看错了方向,但是例如,我有一些类似的思路:

    self.thread = threading.Thread(target=self.run)
    self.thread.setDaemon(True)
    self.thread.start()

    def run(self):
        logging.info("Starting Awesome Program") 
        try:
            while 1:
                awesome_program(self)
    except:
        logging.exception('Got exception on main handler')
        OnError(self)

    def OnError(self):
        self.Destroy()

Obviously I am currently just killing the program when an error is reached. 显然,我目前只是在遇到错误时终止该程序。 awesome_program is basically using pyodbc to connect and run queries on a remote database. awesome_program基本上是使用pyodbc连接并在远程数据库上运行查询。 The problem arises when connection is lost. 连接断开时会出现问题。 If I don't catch the exceptions the program just freezes so I set it up as it is above which kills the program but this is not always ideal if no one is around to manually restart it. 如果我没有捕获到异常,则程序会冻结,因此我将其设置为高于该值,这会杀死该程序,但是如果没有人手动重启它,这并不总是理想的。 Is there an easy way to either keep the program running or restert it. 有没有一种简单的方法来保持程序运行或重新安装它。 Feel free to berate me for incorrect syntax or poor programming skills. 随意指责我语法错误或编程技能差。 I am trying to teach myself and am still very much a novice and there is plenty I don't understand or am probably not doing correctly. 我正在努力自学,现在仍然是一个新手,有很多我不理解或做得可能不正确。 I can post more of the code if needed. 如果需要,我可以发布更多代码。 I wasn't sure how much to post without being overwhelming. 我不确定要发布多少内容而不被压倒。

Catch the exception within the loop, and continue, even if an exception is caught. 在循环内捕获异常,然后继续,即使捕获到异常也是如此。

def run(self):
        logging.info("Starting Awesome Program") 
        while 1:
            try:
                awesome_program(self)
            except:
                logging.exception('Got exception on main handler')
                OnError(self)

BTW: 顺便说一句:

  • Your indentation seems messed up. 您的缩进似乎搞砸了。
  • I'd prefer while True . 我更喜欢while True Python has bool type, unlike C, so when a bool is expected - give while a bool. Python有bool型,不像C,所以当一个bool预期-给while一个布尔值。

You're looking for this: 您正在寻找:

def run(self):
     while True:
         try:
             do_things()
         except Exception as ex:
             logging.info("Caught exception {}".format(ex))

Take a look at Python Exception Handling , and in particular Try...Except . 看一下Python异常处理 ,尤其是Try...Except It will allow you to catch particular errors and handle them however you choose fit, even ignore them completely, if possible. 它将使您能够捕获特定的错误并进行处理,但是您可以选择适当的方法,甚至在可能的情况下完全忽略它们。 For example: 例如:

try:
    while something == True:
        do_stuff()
except ExceptionType:
    print "Something bad happened!" #An error occurred, but the script continues
except:
    print "Something worse happened!"
    raise #a worse error occurred, now we kill it
do_more_stuff()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM