Borealis
Borealis

Reputation: 8480

How to catch specific ExecuteError in Python?

I am getting a specific error in one of my scripts:

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

I have isolated these instances using a try/except block:

try:
    #Do something

except:
    #Do something in the event of failure

How can I trap the specific ExecuteError described above within the except statement?

Upvotes: 1

Views: 387

Answers (2)

Joohwan
Joohwan

Reputation: 2512

Kind of hacky but if you want to catch 000229 only you could do something like this:

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

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799210

You can't. But you can check the various attributes of the exception object to see if it's the one you care about, and reraise the exception otherwise.

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

Upvotes: 2

Related Questions