【问题标题】:How to catch specific ExecuteError in Python?如何在 Python 中捕获特定的 ExecuteError?
【发布时间】:2014-10-10 04:22:02
【问题描述】:

我的一个脚本出现特定错误:

ExecuteError: ERROR 000229: Cannot open F:\path\to\file.tif

我已经使用 try/except 块隔离了这些实例:

try:
    #Do something

except:
    #Do something in the event of failure

如何在except 语句中捕获上述特定的 ExecuteError?

【问题讨论】:

    标签: python error-handling try-except


    【解决方案1】:

    你不能。但是您可以检查异常对象的各种属性以查看它是否是您关心的对象,否则重新引发异常。

    try:
       ...
    except ExecuteError as e:
      if not can_handle(e):
        raise
      handle(e)
    

    【讨论】:

      【解决方案2】:

      有点老套,但如果你只想抓住 000229,你可以这样做:

      try:
          # your code
      except ExecuteError as err:
          if str(err.message).startswith("ERROR 000229"):
              # do something
          else:
              # do something else
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-12
        • 2019-02-20
        • 2014-12-04
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 2020-12-06
        • 1970-01-01
        相关资源
        最近更新 更多