Ignore errors instead of stopping in modelbuilder

1962
12
Jump to solution
03-04-2013 06:10 AM
TomMagdaleno
Regular Contributor
I have a model which will pull 30 features from a database and a second model which will perform a feature class rename on these 30 different features.  This is done on a daily basis.  Sometimes one of the names will change or a feature won't be pulled in for some reason. 
  The problem is that when the rename model is running if it can't find a feature it will error the entire model instead of ignoring the error and skipping to the next feature to rename.  None of the processes have preconditions. 
Is their a way I can tell the model to ignore errors and move on if it can't find a feature?  A green error instead of a red one?
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Wow, that is a lot of feature classes to rename.  That is messy.  Unfortunately, there is no quick way to fix this with so many hard coded values.  Two of the easiest options would be to use a dictionary to handle the naming, or to put a try and except block around each of the rename functions.  Option A would be to create a dictionary to handle this.  For example:

dictionary = {'old_name' : 'new_name'}

If you wanted to take this approach you would need to fill in just the names of the old feature class, then the name of the new feature class inside the dictionary variable.  This is the more pythonic way...

import arcpy, os arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb'  # put all the values in here      'old' : 'new' dictionary = {GIS_CE_' : 'HTE_CodeEnforcement', 'GIS_BP_' : 'HTE_BuildingPermits', ...etc...}  # this will loop thru the whole gdb and change any names that are in the dictionary variable for fc in arcpy.ListFeatureClasses():     if fc in dictionary:         old = os.path.join(ws,fc)         new = os.path.join(ws,dictionary[fc])         try:             arcpy.Rename_management(old, new, "FeatureClass")             print 'Renamed %s to %s' %(fc, dictionary[fc])         except:             print 'Could not rename %s' % old



If you don't feel comfortable with that, the easier way would be to put every occurrence of the rename_management tool inside of a try\except block:

try:     arcpy.Rename_management(GIS_BP_, HTE_BuildingPermits, "FeatureClass") except:      print 'Could not find feature class'      try:     arcpy.Rename_management(GIS_CE_, HTE_CodeEnforcement, "FeatureClass") except:      print 'Could not find feature class'      #  etc........


Either way you do this it will be time consuming.  That is one of the downsides of exporting models to Python script- it will hard code everything.  I am not sure either of these will be saving you any time but both should work if you still want to go the python route.  I am curious as to what is crashing model builder though.  Maybe you have misspelled one of the feature classes in your model?  I would take a look at that before trying either of the above.

EDIT: after reading your first post again, it may be worth it to try the first method and create a dictionary if this will need to be done on a daily basis.  You can always add/change/remove items in the dictionary whenever you need to.  Then you can set this script to run as a scheduled task.

View solution in original post

0 Kudos
12 Replies
by Anonymous User
Not applicable
You could try exporting it to a python script to add some error handling.  For example:

try:
    # rename fc here
except: 
    arcpy.AddMessage('Could not find feature class')


here is another option:
if arcpy.Exists(feature_class):
    # rename fc here
else: 
    arcpy.AddMessage('Could not find feature class')
    # or pass
0 Kudos
TomMagdaleno
Regular Contributor
Thanks for the reply Caleb.  I have never had luck exporting to python and trying to run a script, something is always missing.  So I have my script running ArcCatalog Models instead.  But if I did use Python for this task where would I stick the piece of code you recommended?  I tried and it told me it is expecting an indented block.


# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# HTErename2.py
# Created on: 2013-03-04 13:45:45.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: 
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

try:
    # rename fc here
except: 
    arcpy.AddMessage('Could not find feature class')

# Local variables:
GIS_LX_Active_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_LX_Active_"
GIS_BP_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_BP_"
GIS_CE_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_CE_"
GIS_LX_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_LX_"
GIS_OL_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_OL_"
abc = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\abc"
GIS_PZ_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_PZ_"
GIS_TSI_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_TSI_"
GIS_UA_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UA_"
GIS_UF_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UF_"
GIS_UL_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UL_"
GIS_UR_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UR_"
GIS_URSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_URSVC_"
GIS_US_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_US_"
GIS_USSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_USSVC_"
GIS_UW_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UW_"
GIS_UWSVC_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_UWSVC_"
GIS_WF_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_WF_"
GIS_WF_SWID2_ = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\GIS_WF_SWID2_"
intersections = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\intersections"
HTE_BuildingPermits = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_BuildingPermits"
HTE_CodeEnforcement = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_CodeEnforcement"
HTE_LX_LocPoints_All = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_LX_LocPoints_All"
HDL_BusinessLicense_OL = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HDL_BusinessLicense_OL"
HTE_LX_Active_LocPoints = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_LX_Active_LocPoints"
HTE_ABCLicenses = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_ABCLicenses"
HTE_Planning_Zoning = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Planning_Zoning"
HTE_UtilitiesAll_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_UtilitiesAll_CX"
SQL_TrafficSignInv = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\SQL_TrafficSignInv"
HTE_Util_Fireline_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Fireline_CX"
HTE_Util_Landscape_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Landscape_CX"
HTE_Util_Refuse_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Refuse_Active_CX"
HTE_Util_Sewer_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Sewer_Active_CX"
HTE_Util_Sewer_Service_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Sewer_Service_All_CX"
HTE_Util_Water_Active_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Water_Active_CX"
HTE_Util_Water_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Water_All_CX"
HTE_WorkOrders = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_WorkOrders"
HTE_StormWater_WorkOrders_SWID2 = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_StormWater_WorkOrders_SWID2"
HTE_Intersections = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Intersections"
HTE_Util_Refuse_All_CX = "G:\\CamarilloGIS\\Databases\\GeoDatabases\\HTE.gdb\\HTE_Util_Refuse_All_CX"

try:
    # rename fc here
except: 
    arcpy.AddMessage('Could not find feature class')
    
# Process: Rename (2)
arcpy.Rename_management(GIS_BP_, HTE_BuildingPermits, "FeatureClass")

# Process: Rename (3)
arcpy.Rename_management(GIS_CE_, HTE_CodeEnforcement, "FeatureClass")

# Process: Rename (4)
arcpy.Rename_management(GIS_LX_, HTE_LX_LocPoints_All, "FeatureClass")

# Process: Rename (5)
arcpy.Rename_management(GIS_OL_, HDL_BusinessLicense_OL, "FeatureClass")

# Process: Rename (7)
arcpy.Rename_management(GIS_LX_Active_, HTE_LX_Active_LocPoints, "FeatureClass")

# Process: Rename (6)
arcpy.Rename_management(abc, HTE_ABCLicenses, "FeatureClass")

# Process: Rename (8)
arcpy.Rename_management(GIS_PZ_, HTE_Planning_Zoning, "FeatureClass")

# Process: Rename (9)
arcpy.Rename_management(GIS_UA_, HTE_UtilitiesAll_CX, "FeatureClass")

# Process: Rename (10)
arcpy.Rename_management(GIS_TSI_, SQL_TrafficSignInv, "FeatureClass")

# Process: Rename (11)
arcpy.Rename_management(GIS_UF_, HTE_Util_Fireline_CX, "FeatureClass")

# Process: Rename (12)
arcpy.Rename_management(GIS_UL_, HTE_Util_Landscape_CX, "FeatureClass")

# Process: Rename (13)
arcpy.Rename_management(GIS_UR_, HTE_Util_Refuse_Active_CX, "FeatureClass")

# Process: Rename (14)
arcpy.Rename_management(GIS_US_, HTE_Util_Sewer_Active_CX, "FeatureClass")

# Process: Rename (15)
arcpy.Rename_management(GIS_USSVC_, HTE_Util_Sewer_Service_All_CX, "FeatureClass")

# Process: Rename (16)
arcpy.Rename_management(GIS_UW_, HTE_Util_Water_Active_CX, "FeatureClass")

# Process: Rename (17)
arcpy.Rename_management(GIS_UWSVC_, HTE_Util_Water_All_CX, "FeatureClass")

# Process: Rename (18)
arcpy.Rename_management(GIS_WF_, HTE_WorkOrders, "FeatureClass")

# Process: Rename (19)
arcpy.Rename_management(GIS_WF_SWID2_, HTE_StormWater_WorkOrders_SWID2, "FeatureClass")

# Process: Rename (20)
arcpy.Rename_management(intersections, HTE_Intersections, "FeatureClass")

# Process: Rename (22)
arcpy.Rename_management(GIS_URSVC_, HTE_Util_Refuse_All_CX, "FeatureClass")

0 Kudos
by Anonymous User
Not applicable
Wow, that is a lot of feature classes to rename.  That is messy.  Unfortunately, there is no quick way to fix this with so many hard coded values.  Two of the easiest options would be to use a dictionary to handle the naming, or to put a try and except block around each of the rename functions.  Option A would be to create a dictionary to handle this.  For example:

dictionary = {'old_name' : 'new_name'}

If you wanted to take this approach you would need to fill in just the names of the old feature class, then the name of the new feature class inside the dictionary variable.  This is the more pythonic way...

import arcpy, os arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb'  # put all the values in here      'old' : 'new' dictionary = {GIS_CE_' : 'HTE_CodeEnforcement', 'GIS_BP_' : 'HTE_BuildingPermits', ...etc...}  # this will loop thru the whole gdb and change any names that are in the dictionary variable for fc in arcpy.ListFeatureClasses():     if fc in dictionary:         old = os.path.join(ws,fc)         new = os.path.join(ws,dictionary[fc])         try:             arcpy.Rename_management(old, new, "FeatureClass")             print 'Renamed %s to %s' %(fc, dictionary[fc])         except:             print 'Could not rename %s' % old



If you don't feel comfortable with that, the easier way would be to put every occurrence of the rename_management tool inside of a try\except block:

try:     arcpy.Rename_management(GIS_BP_, HTE_BuildingPermits, "FeatureClass") except:      print 'Could not find feature class'      try:     arcpy.Rename_management(GIS_CE_, HTE_CodeEnforcement, "FeatureClass") except:      print 'Could not find feature class'      #  etc........


Either way you do this it will be time consuming.  That is one of the downsides of exporting models to Python script- it will hard code everything.  I am not sure either of these will be saving you any time but both should work if you still want to go the python route.  I am curious as to what is crashing model builder though.  Maybe you have misspelled one of the feature classes in your model?  I would take a look at that before trying either of the above.

EDIT: after reading your first post again, it may be worth it to try the first method and create a dictionary if this will need to be done on a daily basis.  You can always add/change/remove items in the dictionary whenever you need to.  Then you can set this script to run as a scheduled task.
0 Kudos
TomMagdaleno
Regular Contributor
Thanks for trying Caleb.  I think I will just refine the model instead of trying to recode everything.
0 Kudos
by Anonymous User
Not applicable
Thanks for trying Caleb.  I think I will just refine the model instead of trying to recode everything.


There may be something in Model Builder that is similar to the try\except; I do not use it too much so I am not sure.  I think this should work though. Open up a new IDLE and go to File > New Window and paste this code (be sure to save the script with a .py extension):

import arcpy, os
arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb'

namedict = {'GIS_BP_': 'HTE_BuildingPermits','GIS_CE_': 'HTE_CodeEnforcement',
            'GIS_LX_': 'HTE_LX_LocPoints_All','GIS_OL_': 'HDL_BusinessLicense_OL',
            'GIS_LX_Active_': 'HTE_LX_Active_LocPoints','abc': 'HTE_ABCLicenses',
            'GIS_PZ_': 'HTE_Planning_Zoning','GIS_UA_': 'HTE_UtilitiesAll_CX',
            'GIS_TSI_': 'SQL_TrafficSignInv','GIS_UF_': 'HTE_Util_Fireline_CX',
            'GIS_UL_': 'HTE_Util_Landscape_CX','GIS_UR_': 'HTE_Util_Refuse_Active_CX',
            'GIS_US_': 'HTE_Util_Sewer_Active_CX','GIS_USSVC_': 'HTE_Util_Sewer_Service_All_CX',
            'GIS_UW_': 'HTE_Util_Water_Active_CX','GIS_UWSVC_': 'HTE_Util_Water_All_CX',
            'GIS_WF_': 'HTE_WorkOrders','GIS_WF_SWID_': 'HTE_StormWater_WorkOrders_SWID',
            'intersections': 'HTE_Intersections','GIS_URSVC_': 'HTE_Util_Refuse_All_CX',}

# Loop thru gdb
for fc in arcpy.ListFeatureClasses():
    if fc in dictionary:
        old = os.path.join(ws,fc)
        new = os.path.join(ws,dictionary[fc])
        try:
            arcpy.Rename_management(old, new, "FeatureClass")
            print 'Renamed %s to %s' %(fc, dictionary[fc])
        except:
            print 'Could not rename %s' % old
0 Kudos
TomMagdaleno
Regular Contributor
Caleb,
  I get this error.  How do I define the dictionary?

Traceback (most recent call last):
  File "C:/01Working_Directory/Python/HTErenamePythontest", line 17, in <module>
    if fc in dictionary:
NameError: name 'dictionary' is not defined
0 Kudos
by Anonymous User
Not applicable
Caleb,
  I get this error.  How do I define the dictionary?

Traceback (most recent call last):
  File "C:/01Working_Directory/Python/HTErenamePythontest", line 17, in <module>
    if fc in dictionary:
NameError: name 'dictionary' is not defined


Whoops, that was my fault, I had namedict for the name of the dictionary ( I copied and pasted the code from the first example I gave after creating the dictionary).  Try this:

import arcpy, os
arcpy.env.workspace = ws = r'G:\CamarilloGIS\Databases\GeoDatabases\HTE.gdb'

namedict = {'GIS_BP_': 'HTE_BuildingPermits','GIS_CE_': 'HTE_CodeEnforcement',
            'GIS_LX_': 'HTE_LX_LocPoints_All','GIS_OL_': 'HDL_BusinessLicense_OL',
            'GIS_LX_Active_': 'HTE_LX_Active_LocPoints','abc': 'HTE_ABCLicenses',
            'GIS_PZ_': 'HTE_Planning_Zoning','GIS_UA_': 'HTE_UtilitiesAll_CX',
            'GIS_TSI_': 'SQL_TrafficSignInv','GIS_UF_': 'HTE_Util_Fireline_CX',
            'GIS_UL_': 'HTE_Util_Landscape_CX','GIS_UR_': 'HTE_Util_Refuse_Active_CX',
            'GIS_US_': 'HTE_Util_Sewer_Active_CX','GIS_USSVC_': 'HTE_Util_Sewer_Service_All_CX',
            'GIS_UW_': 'HTE_Util_Water_Active_CX','GIS_UWSVC_': 'HTE_Util_Water_All_CX',
            'GIS_WF_': 'HTE_WorkOrders','GIS_WF_SWID_': 'HTE_StormWater_WorkOrders_SWID',
            'intersections': 'HTE_Intersections','GIS_URSVC_': 'HTE_Util_Refuse_All_CX'}

# Loop thru gdb
for fc in arcpy.ListFeatureClasses():
    if fc in namedict:
        old = os.path.join(ws,fc)
        new = os.path.join(ws,namedict[fc])
        try:
            arcpy.Rename_management(old, new, "FeatureClass")
            print 'Renamed %s to %s' %(fc, namedict[fc])
        except:
            print 'Could not rename %s' % old
0 Kudos
TomMagdaleno
Regular Contributor
Still no dice and I can't figure out why.  This is why I use a batch file to open up ArcCatalog and run modelbuilder, its much easier.
0 Kudos
by Anonymous User
Not applicable
Still no dice and I can't figure out why.  This is why I use a batch file to open up ArcCatalog and run modelbuilder, its much easier.


Interesting...What error message were you getting?  I made a replica of your geodatabase in the exact same location  on my G: drive and executed the same code I posted above and it worked perfectly (Maybe I had your path name wrong?).  I have attached screenshots of the whole process below.

[ATTACH=CONFIG]22400[/ATTACH][ATTACH=CONFIG]22401[/ATTACH][ATTACH=CONFIG]22402[/ATTACH][ATTACH=CONFIG]22403[/ATTACH]
0 Kudos