jamesfreddyc

Raising exceptions in a Python thread

Discussion created by jamesfreddyc on May 21, 2013
Latest reply on May 26, 2013 by wunderkind
Ok I'm a newbie in this area so I need to understand how exceptions are raised...
Below is my Add-in code which is essentially the example for a button on a toolbar in the ArcGIS 10.1 Python wizard help.
I've added the error handling.....
So the problem is that nothing happens - the button does not work but neither are there any exceptions raised...
So what am I doing wrong? The add-in toolbar appears in the project, the button is enabled but no exceptions appear in the python window and I have no indication the code is executing.

import arcpy
import pythonaddins

class ButtonClass11(object):
    """Implementation for test_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
try:
# Implementation of OnClick method of Button's class
    def onClick(self):
        # Get the current map document and the first data frame.
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        # Call the zoomToSelectedFeatures() method of the data frame class
        df.zoomToSelectedFeatures()
    pass
except Exception, e:
    traceback.print_exc()


Maybe a dumb question, but do you have any features selected when you click the button?

You can also put a print statement before and after the line you expect to 'do stuff'.  In the code above, you can test the zoomToSelectedFeatures method like so,

print "...attempting to run zoomToSelected"
# Call the zoomToSelectedFeatures() method of the data frame class
df.zoomToSelectedFeatures()
print "...finished running zoomToSelected"


This will output the two statements in the Python window when you click the add-in button.  Just a quick and dirty way to debug.

Outcomes