Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script that creates a new feature using arcpy.GetParameterAsText, but I'm having trouble creating an error message for incorrect user input. The input for a parcel # should be something like '1500300000029', but what if 'sdfjfh' is entered? As it stands no error is thrown and a empty feature is created. I've also tried inside PythonWin by assigning a bad entry for 'input' and it runs without error.

import arcpy
import sys

arcpy.env.overwriteOutput = True
    Parcels = "..path to data"
arcpy.env.workspace = "C:/Data_new/Temp/Default.gdb"

#input = arcpy.GetParameterAsText(0)    
input = "dfdsgdg"                 

expression = "PARCEL_ID = " + "'" + str(input) + "'"
arcpy.MakeFeatureLayer_management(Parcels, "parcels_lyr")
arcpy.SelectLayerByAttribute_management("parcels_lyr", "NEW_SELECTION", expression)   

arcpy.CopyFeatures_management("parcels_lyr", "output")
    print arcpy.GetMessages()

I've also tried

except StandardError, ErrDesc:


What is the appropriate way to handle this?

share|improve this question
I just use GetCount to see whether what is passed in found any features and then act accordingly. You may want to also use Tool Validation. – PolyGeo May 23 '13 at 12:49
i altered my code using your advice and it works when I run inside PythonWin, but nothing is presented when ran inside ArcMap. isValid = int(arcpy.GetCount_management("parcels_lyr").getOutput(0)) if isValid < 1: raise ValueError else: arcpy.CopyFeatures_management("parcels_lyr", "output") except ValueError: print "Oops!" – KFP May 23 '13 at 13:27
Nevermind....Except: arcpy.AddWarning("Invalid Entry.") did the trick. thanks!! – KFP May 23 '13 at 13:32

When running something like this in a Toolbox Tool, you indeed want to use Tool Validation. For example:

class ToolValidator:
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    import arcpy
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if self.params[0].value:
      is_a_number = self.is_number(self.params[0].value)
      if not is_a_number:
        self.params[0].setErrorMessage("Input is not a number")

  def is_number(self, string_to_test):
        return True
    except ValueError:
        return False

When run with a string entered as the input, it gives a error message to the user, prohibiting them from moving on without fixing the problem:

enter image description here

Tool Validators are great, you can use them for testing all kinds of stuff such as the datum or spatial reference of an layer, folder and file names (using regex is you want), or just about anything.

share|improve this answer
Thanks. I'll definitely refer to this in the future. But my input string can include letters as well (1500700000035A) for example, so I couldn't check for only numbers. I thought about checking for correct length, but (1500700000035) is also a valid entry. – KFP May 23 '13 at 14:38
In that case, you probably just want to use a simple regular expression like ^\d*\w$ (untested) to validate the input string. – Chad Cooper May 23 '13 at 15:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.