Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要运行一个 python 脚本每小时向我发送一封电子邮件,其中包含一些日志信息。问题是服务器暂时失败,然后我的 python 脚本返回到 bash。
我尝试了一下,但是当连接断开时它仍然返回到 bash。
使用 try..except 语句:
while True: try: send(text) except Exception as e: logger.log(e)
您可以在 try 和 except 语句中嵌入代码。所以它将是:
while True: try: # Your code except: pass
此处代码将继续运行,如果遇到错误,它将不会执行您的代码,但 while 循环将继续。