talking to arcgisscripting.ExecuteError in custom python tool

2965
3
Jump to solution
03-12-2012 12:32 PM
MelindaMorang1
New Contributor III
I've created a custom tool using python geoprocessing, and it contains a bunch of Network Analyst tools.

At one point in my code, I calculate an OD Cost Matrix and then use the results to do other stuff.  However, it is conceivable that the OD Cost Matrix could yield no results, which leads to a "No Solution Found" error.

<class 'arcgisscripting.ExecuteError'>: ERROR 030024: Solve returned a failure.
No solution found.
Failed to execute (Solve).

The tool then crashes and exits.

However, when this case occurs, I'd rather simply alert the user and then have the tool continue the analysis in a different way.  So I want to include a logic statement.  However, I can't figure out how to actually talk to this error message/exception within python.

How do I code something that does this:
if ("No Solution Found" = TRUE):
   Do this...

Thanks!
0 Kudos
1 Solution

Accepted Solutions
markdenil
Occasional Contributor III
A try / except struture allows you to identify named exceptions (and identify multiple named exceptions) and catch them with an appropriate handler.

You can either assign a single except routine to multiple named exceptions, or different except routines to different errors.

try:     #do stuff except 'arcgisscripting.ExecuteError':     continue except (differentError1, differentError2):     pass

View solution in original post

0 Kudos
3 Replies
markdenil
Occasional Contributor III
A try / except struture allows you to identify named exceptions (and identify multiple named exceptions) and catch them with an appropriate handler.

You can either assign a single except routine to multiple named exceptions, or different except routines to different errors.

try:     #do stuff except 'arcgisscripting.ExecuteError':     continue except (differentError1, differentError2):     pass
0 Kudos
MelindaMorang1
New Contributor III
You can either assign a single except routine to multiple named exceptions, or different except routines to different errors.


Thanks, mdenil!  That makes sense, and I have no idea why I didn't think of that myself.

What's the syntax for the particular error I'm looking for? I know there are several reasons I could get an "ERROR 030024"  I'd want the code to fail for anything other than "No solution found."
0 Kudos
markdenil
Occasional Contributor III
Use a general except clause with
print arcpy.GetMessages(3)
so arcpy reports the actual error returned.

run the script so it fails on the error you want to identify.
arcpy should report the error you want to make a named exception for.
0 Kudos