如何忽略错误并继续执行脚本的其余部分

编程入门 行业动态 更新时间:2024-10-28 16:29:32
本文介绍了如何忽略错误并继续执行脚本的其余部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

一些背景知识。我想删除AWS红移集群,这个过程可能需要超过30分。所以,这就是我想要做的:

Some background. I want to delete AWS Redshift cluster and the process takes more than 30 minute. So this is what I want to do:

  • 开始删除
  • 在每1分钟,确认集群的状态(这应该是删除)
  • 当集群被删除,该命令会失败(因为它 找不到群集了)。因此,记录一些消息,并继续执行脚本的其余部分
  • Start the deletion
  • Every 1 minute, check the cluster status (it should be "deleting")
  • When the cluster is deleted, the command would fail (because it cannot find the cluster anymore). So log some message and continue with rest of the script
  • 这是我在,而循环来检查群集状态运行该命令后,我开始删除:

    This is the command I run in a while loop to check the cluster status after I start the deletion:

    resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah")

    上面的命令会得到我的群集状态为删除而删除过程仍在继续。但是,一旦集群被完全删除,那么该命令本身会失败,因为它无法找到集群 blahblahblah 。

    Above command will get me cluster status as deleting while the deletion process continues. But once the cluster is completely deleted, then the command itself will fail as it cannot find the cluster blahblahblah.

    下面是命令的错误,一旦集群被删除:

    Here is the error from command once the cluster is deleted:

    /var/lib/gems/1.9.1/gems/aws-sdk-1.14.1/lib/aws/core/client.rb:366:in `return_or_raise': Cluster blahblahblah not found. (AWS::Redshift::Errors::ClusterNotFound)

    我同意这种错误。但是,这让我的脚本退出突然。所以,我要记录一个消息,说集群被删除....继续,并继续我的脚本。

    I agree with this error. But this makes my script exit abruptly. So I want to log a message saying The cluster is deleted....continuing and continue with my script.

    我试过下面设置

    resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah") || raise ("The cluster is deleted....continuing")

    在 www.ruby-forum/提到

    我也试过情侣暗示主题/ 133876 但是,这是行不通的。上述命令未能找到集群脚本我退出一次。

    I also tried couple of suggestion mentioned at www.ruby-forum/topic/133876 But this is not working. My script exits once above command fails to find the cluster.

    问题: 如何忽略错误,打印自己的消息说群集被删除....继续并继续脚本?

    Questions: How to ignore the error, print my own message saying "The cluster is deleted....continuing" and continue with the script ?

    感谢。

    推荐答案

    @NewAlexandria,我改变了你的code看起来像如下:

    @NewAlexandria, I changed your code to look like below:

    puts "Checking the cluster status" begin resp = redshift.client.describe_clusters(:cluster_identifier=>"blahblahblah") rescue AWS::Redshift::Errors::ClusterNotFound => cluster_exception puts "The cluster is deleted....continuing" end puts "seems like the cluster is deleted and does not exist"

    输出:

    Checking the cluster status The cluster is deleted....continuing seems like the cluster is deleted and does not exist

    我改变了募集到看跌在紧跟在救援线在你的回应。这样,我摆脱了 RuntimeError 我提到我上面的评论。

    I changed the raise to puts in the line that immediately follows the rescue line in your response. This way I got rid of the RuntimeError that I mentioned in my comment above.

    我不知道这有什么寓意。我甚至不知道这是否是正确的方式来做到这一点。但它示出了当未找到的群集的误差,然后与脚本继续

    I do not know what are the implication of this. I do not even know whether this is the right way to do it. But It shows the error when the cluster is not found and then continues with the script.

    后来我看了很多文章,红宝石例外/救援/升/扔 ....但是那只是对我来说太多了解,因为我不属于编程背景的。所以,如果你能解释一下是怎么回事,它会非常有帮助,我得到更多的信心,红宝石。

    Later I read a lot of articles on ruby exception/rescue/raise/throw.... but that was just too much for me to understand as I do not belong to programming background at all. So, if you could explain what is going on here, it will really helpful for me to get more confidence in ruby.

    感谢您的时间。

    更多推荐

    如何忽略错误并继续执行脚本的其余部分

    本文发布于:2023-10-24 07:50:35,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1523359.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:脚本   错误

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!