【问题标题】:Does a try except stop right away in python if an exception is found?如果发现异常,是否会立即在 python 中停止尝试?
【发布时间】:2020-06-26 03:16:27
【问题描述】:

如果我的 python 代码中有一个 try except 块,并且我的 try 语句的第一行引发异常,它会自动转到异常还是先完成 try 块?

try:
    int(string)
    string = "This was a mistake, can't int string"
except:
    pass

这里是检查它是否可以int(string),它不能,然后它立即移动到except,还是它先进行字符串赋值?

当我运行它时,它似乎立即停止了,但我想知道这是肯定发生的还是其他原因。

谢谢

【问题讨论】:

  • 这是否回答了您的问题。 stackoverflow.com/questions/19522990/…
  • 你的想法是对的。当遇到异常时,它会立即转到 except 块,并且不会运行其余代码。但你已经知道了!你运行它,你看到它做了什么。相信代码。这就是它的工作原理,当你已经这样做时,你不需要互联网上的人来确认!
  • 是的,对不起,我只是用python构建我的第一个项目,不想搞砸!

标签: python


【解决方案1】:

让我们试试吧。

try:
    1/0
    print("what?")
except:
    print("nope")

输出:

nope
>>> 

当引发异常时,它会转到异常。

【讨论】:

    【解决方案2】:

    希望对你有帮助!

    #the try block is to run the process
        try:
            string = "This was a mistake, can't int string"
            #you are parsing the string into an int 
            int(string)
        
        #this block is to capture the exception when it is raised
        except:
            #here I am just printing the exception 
            print('Exception')
            #the pass is to pass the execution and follow the remaining process.
            pass
    

    输出:

    Exception
    >>>
    

    【讨论】:

      【解决方案3】:

      从pythondocs,我找到了这个,

      "The try statement works as follows.
      
      First, the try clause (the statement(s) between the try and except keywords) is executed.
      
      If no exception occurs, the except clause is skipped and execution of the try statement is finished.
      
      If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try statement.
      
      If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with a message as shown above."
      

      干杯!

      【讨论】:

        【解决方案4】:

        您的程序似乎有问题,也许您对 try except 块的理解。

        首先,从您提供的代码块来看,在我看来,当您尝试传递int(string) 时,甚至还没有定义字符串。

        其次,在我看来,您似乎正在定义的 string 变量似乎是您在程序吐出错误并传递到 except 块时尝试打印到控制台的东西。如果这是您想要做的,那么您的代码应该如下所示:

        try:
            int(string)
        except:
            print("This was a mistake, can't int string")
        

        如果您要运行上面提供的代码,您确实会确认 try except 语句正在工作,因为它将"This was a mistake, can't int string" 打印到控制台而不是给出错误。希望这有点帮助和可以理解。你得到了这个酋长,继续努力!

        【讨论】:

          【解决方案5】:

          好吧,这就是错误处理程序的全部意义所在。当out程序发生错误时,它会崩溃,它不会简单地跳过错误行。

          try:
              int(string)
              string = "This was a mistake, can't int string"
          except:
              pass
          

          因此,如果 try 块即使在中间发生错误之后也完成了,那么错误处理程序似乎有点毫无意义。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-02-04
            • 1970-01-01
            • 2014-07-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多