Select to view content in your preferred language

Can't access to the errors with GetMessages()

2483
7
10-25-2012 09:55 AM
by Anonymous User
Not applicable
Greetings to all

I'm not getting access to the error messages with arcpy.GetMessages arcpy () and related functions. I'm really frustrated with this problem. For example, if I run on the Windows:

arcpy.GetCount_management ("c :/ temp/solapes2.shp")


(where there is not solapes2.shp shape at the location indicated)

I get the following error message:

<class'arcgisscripting.ExecuteError'> Runtime error: ERROR 000732: Input Rows: c :/ temp/solapes2.shp the dataset does not exist or does not support


But if I execute, for example:

import arcpy
try:
**** result = arcpy.GetCount_management ("c :/ temp/solapes2.shp")
except:
arcpy.GetMessageCount print ()
arcpy.GetMessages print (2)


I only get:

0



(that is, a zero from GetMessageCount and a blank line from getMessages (2).

Where is the problem? Am I wrong with the way errors are handled in the Python window? I tried several scripts that appear in the Help and their behavior is similar to that describe ....

Thanks in advance

Jesús de Diego
Pd. I'm using ArcGIS 10.0, sp5. in a Windows 7 64 bits OS.
Tags (2)
0 Kudos
7 Replies
JillianPenney
Esri Contributor
Does something like this work if you try it?

[HTML]
import arcpy
arcpy.GetCount_management("C:/temp/solapes2.shp")
print arcpy.GetMessages()
[/HTML]

The spaces in the path to the shapefile may be cause of the first error.
0 Kudos
by Anonymous User
Not applicable
Hello Jill

Thank you for your answer. However, what I really want is force the error... What I want to ask is why the GetMessages function is not showing any error...

Thanks

Jesús
0 Kudos
KevinHibma
Esri Regular Contributor
I'd have a look through this topic....
http://resources.arcgis.com/en/help/main/10.1/#/Understanding_message_types_and_severity/002z0000000...

But what I think is happening.... you're trying to make the tool "error". The tool hasn't actually errored because it hasn't run.
The GetMessages is used to get messages from the tool execution (which hasnt occurred). You're seeing a validation error.
0 Kudos
by Anonymous User
Not applicable
Hello Kevin

mm.... but... when can I get severity 2 messages if I use arcpy.GetMessages(2).

And also, if I want the get error messages in the except clause, the only way is capturing the exception message? Like this way?

except Exception as e:
  print e.message

Thank you in advance

Jesús de Diego
0 Kudos
curtvprice
MVP Alum
If I want the get error messages in the except clause, the only way is capturing the exception message?  Like this way?

except Exception as e:
    print e.message


Yes.

import arcpy
try:
    result = arcpy.GetCount_management("c:/temp/solapes2.shp")
    # print tool messages if successful
    arcpy.AddMessage(arcpy.GetMessages(0)) # to ArcGIS (script tool)
    print arcpy.GetMessages(0) # to stdout
except arcpy.ExecuteError:
    # tool failed
    arcpy.AddError(arcpy.GetMessages(0)) # to ArcGIS
    print arcpy.GetMessages(0) # to stdout
except Exception, msg:
    # other errors (say, Python syntax)
    arcpy.AddError(str(msg)) # to ArcGIS
    print arcpy.GetMessages(0) # to stdout


There are some nice examples here:

Arc 10 help: Error handling with Python
by Anonymous User
Not applicable
Yes.

import arcpy
try:
    result = arcpy.GetCount_management("c:/temp/solapes2.shp")
    # print tool messages if successful
    arcpy.AddMessage(arcpy.GetMessages(0)) # to ArcGIS (script tool)
    print arcpy.GetMessages(0) # to stdout
except arcpy.ExecuteError:
    # tool failed
    arcpy.AddError(arcpy.GetMessages(0)) # to ArcGIS
    print arcpy.GetMessages(0) # to stdout
except Exception, msg:
    # other errors (say, Python syntax)
    arcpy.AddError(str(msg)) # to ArcGIS
    print arcpy.GetMessages(0) # to stdout


There are some nice examples here:

Arc 10 help: Error handling with Python


First of all, thank you for your answer, Curtis.

arcpy.AddError is working fine for script based toolboxes, no problem with this. However, arcpy.GetMessages is not working as I suppossed. Why GetMessages is working in a Python IDE (like WingIDE) and not in ArcGIS Python Windows?

Thank you

Jesús de Diego
0 Kudos
curtvprice
MVP Alum
However, arcpy.GetMessages is not working as I suppossed. Why GetMessages is working in a Python IDE (like WingIDE) and not in ArcGIS Python Windows?


Messages are handled differently in the ArcGIS Desktop python window. For example AddError and AddMessage do not work there, since tools display those messages without you having to explicitly tell it to. Print works though. I have found this in other IDEs as well, sometimes the arcpy.Add... messages do not appear.

Entered the following in the ArcMap Python window. Note AddError does nothing.

>>> try:
...     raise
... except:
...     print "printprint"
...     arcpy.AddError("errorprint")
...     
printprint
>>> 
0 Kudos