V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
pureGirl
V2EX  ›  程序员

Python 中 try except 里是不是不能用 continue

  •  
  •   pureGirl · Feb 13, 2025 · 1832 views
    This topic created in 443 days ago, the information mentioned may be changed or developed.

    试了一下在 try 中 continue ,本次循环还是会正常执行。但是 if 中使用 continue ,就会跳过本次进行下一次的循环。

    7 replies    2025-02-13 11:16:10 +08:00
    nagisaushio
        1
    nagisaushio  
       Feb 13, 2025 via Android
    展示代码
    pureGirl
        2
    pureGirl  
    OP
       Feb 13, 2025
    while:
    pureGirl
        3
    pureGirl  
    OP
       Feb 13, 2025
    while True:
    try:
    print("尝试执行")
    error_message = None # 假设没有找到错误
    continue
    except Exception:
    print("异常捕获")

    print("这行会执行") # 没有 exception 也会执行
    Tanix2
        4
    Tanix2  
       Feb 13, 2025
    我试了,本次循环就跳过了,没有你所说的情况
    yankebupt
        5
    yankebupt  
       Feb 13, 2025
    建议问 AI
    人的反应是:为什么不把“这行会执行”直接塞进 except 块里……然后没有 continue
    需要展示更多的代码逻辑说明你这样做的理由。
    Courstick
        6
    Courstick  
       Feb 13, 2025
    @pureGirl #3 展示缩进
    iorilu
        7
    iorilu  
       Feb 13, 2025
    ```while True:
    try:
    nums = input('enter 2 number: ')
    a, b = nums.split()
    a = int(a)
    b = int(b)
    print(f' a / b = {a//b}')
    error_message = None # 假设没有找到错误
    continue
    except Exception as e:
    print("异常捕获")
    error_message = str(e)

    print(error_message)```

    我试了, 没问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2283 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 01:15 · PVG 09:15 · LAX 18:15 · JFK 21:15
    ♥ Do have faith in what you're doing.