Select to view content in your preferred language

checking for existence of input data in toolvalidator crashes arcmap

1044
4
08-25-2011 03:45 AM
CrispinCooper
Emerging Contributor
I'm trying to write a toolvalidator which auto-fills various parameters with field names from the selected input feature layer, if the relevant fields exist in the feature layer.

Trouble is, if the user enters the name of a non-existant input feature layer, it crashes all of arcmap!

How can I safely check that the data exists?
I have tried variants on arcpy.Exists and arcpy.Describe (surrounded by try/catch) - all lead to the crash.

    defaultnames = {1:"default_name_1",
                    2:"default_name_2"}

    fcname = self.params[0].value
    if fcname and arcpy.Exists(fcname):
      desc = arcpy.Describe(fcname)
      datatype = desc.dataType
      if datatype=="FeatureClass" or datatype=="FeatureLayer":
        fieldnames = [f.name for f in desc.fields]
        for param_index,name in defaultnames.iteritems():
          if name in fieldnames:
            self.params[param_index].value = name
Tags (2)
0 Kudos
4 Replies
ChrisSnyder
Honored Contributor
Even if you put the entire validation code in a try/except?
0 Kudos
CrispinCooper
Emerging Contributor
Yes, even if it's all in a try/except block.
0 Kudos
CrispinCooper
Emerging Contributor
Ok, some progress on this one.

The crash only happens when the input param type is set to 'layer' rather than 'feature class'.  Some of the other parameters have type 'field' obtained for the initial input param.  Maybe the internal validation code is crashing when trying to list fields in a layer instead of a feature class?

So, put input param type back to 'feature class' and all is well, except the tool is a pain in the backside to use now.  I have started a different thread on how to use layers as inputs.
0 Kudos
CrispinCooper
Emerging Contributor
For anyone watching - after dialog with support, it turns out upgrading to arc 10 service pack 3 fixes this.

http://resources.arcgis.com/content/patches-and-service-packs?fa=viewPatch&PID=17&MetaID=1807#instal...

The line causing the crash was the first one where I read self.params[0].value.
0 Kudos