0

I'm working on some code for a toolbar I made using Python-addin that makes buildings. Part of the code that does this makes a Temporary table view. If my code attempts to make that table view again when it already exists, this error is thrown:

Traceback (most recent call last):


File "filepath", line error happens on, in onClick
    '')
  File "otherfilepath", line I didn't code, in MakeTableView
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Table Name: Dataset editViewStormBill already exists.
Failed to execute (MakeTableView).

I've already made changes in my code to ensure that this error isn't thrown every time a building is drawn. This error is only thrown right now if the user takes a specific action that shows they don't really understand how the toolbar works. What I would like to do is catch the error with an exception that prints out a few lines explaining what the user did wrong and then pass over that part of the code so the rest of the code can attempt to run. I've done this before, but I don't know what to call the error in the try except statement. Here is the bit I'm struggling with:

if button2.isTableView == False:
                try:
                    tableView = arcpy.MakeTableView_management(fc,
                                                "editView" + str(button2.i),
                                               "",workspace,
                                               '')
                    arcpy.ChangeVersion_management((tableView),"TRANSACTIONAL",
                                                       button2.currentVersion,
                                                       '')
                    button2.isTableView = True
                except ???:#What goes here?
                    pass

So far I have tried putting ExecuteError where the questions marks are and nothing happens. What do I put after except; what part of the error tells me what to write for my exception in python?

I'm not overwriting output because that approach would have my code make the tableView every time this button is pressed which would make the toolbar slower and less useful.

If you need to reproduce this error in order to explain how to read error messages then do this:

                try:
                    tableView = arcpy.MakeTableView_management(fc,
                                                "editView",
                                               "",workspace,
                                               '')
                    tableView = arcpy.MakeTableView_management(fc,
                                                "editView",
                                               "",workspace,
                                               '')
                    arcpy.ChangeVersion_management((tableView),"TRANSACTIONAL",
                                                       button2.currentVersion,
                                                       '')
                    button2.isTableView = True
                except :#What goes here?
                    pass
  • 1
    I think this is a raw python question, not directly GIS related, and would be best asked on Stack Overflow – Midavalo Feb 6 '17 at 23:31
  • 2
    @Midavalo it's more pythony than it is GIS-y, but python exceptions generally don't work this way from what I've seen. I'm fairly certain that people who don't work with arcpy won't be able to answer this. I need to mention python-addin in this question. – user66821 Feb 6 '17 at 23:35
  • 2
    Some suggestions - arcpy.env.overwriteOutput = True, if arcpy.exists(tableView.getOutput(0)), or a catch all try/except only around the MakeTableView call try: except Exception as e: – user2856 Feb 6 '17 at 23:46
  • 1
    I think we need a code snippet that works up to where you are stuck. I would try to abstract the problem away from a versioned geodatabase into a file geodatabase so that your chances of finding a potential answerer willing to volunteer the testing time will increase. The code snippet should be a whole simple Python AddIn including the script and config.xml. – PolyGeo Feb 7 '17 at 0:08
  • 2
    Just sticking my oar in to say that a plain except: would actually work, but is too blunt a tool to be used in most situations. except arcpy.ExecuteError: is much better. – jon_two Feb 9 '17 at 11:53
4

You can avoid ERROR 000725: Table Name: Dataset editViewStormBill already exists. by setting arcpy.env.overwriteOutput = True

Otherwise you can use arcpy.ExecuteError to catch the exception and arcpy.GetMessages() to print it out.

import arcpy, pythonaddins
#Stuff...

try:
    tableView = arcpy.MakeTableView_management(fc,
        "editView" + str(button2.i), 
        workspace=workspace).getOutput(0)
    arcpy.ChangeVersion_management((tableView),"TRANSACTIONAL",
        button2.currentVersion)
    button2.isTableView = True

except arcpy.ExecuteError:
    #print arcpy.GetMessages()
    pythonaddins.MessageBox(arcpy.GetMessages(), 'Oh dear, you messed up...')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy