Summary
The SeverityLevel class controls how geoprocessing tools raise exceptions.
Discussion
The SeverityLevel class can be used as a function decorator or as a context manager in a with block. When the SeverityLevel class is not used, or when using the severity_level parameter value of 2, tools will only raise an exception when a tool has an error. To control how geoprocessing tools raise exceptions for an entire script, use the SetSeverityLevel function.
Syntax
SeverityLevel (severity_level)
| Parameter | Explanation | Data Type |
severity_level |
Specifies the severity level.
(The default value is 2) | Integer |
Code sample
Use the SeverityLevel class as a context manager to raise an exception when a tool warning is encountered.
import arcpy
fc1 = 'c:/resources/resources.gdb/boundary'
fc2 = 'c:/resources/resources.gdb/boundary2'
with arcpy.SeverityLevel(1):
print(f"Severity is set to : {arcpy.GetSeverityLevel()}")
try:
# FeatureCompare returns warning messages when a miscompare is
# found. This normally would not cause an exception; however, by
# setting the severity level to 1, all tool warnings will also
# return an exception.
arcpy.management.FeatureCompare(fc1, fc2, "OBJECTID")
except arcpy.ExecuteWarning:
print(f"Warning: {arcpy.GetMessages(1)}")
except arcpy.ExecuteError:
print(f"Error: {arcpy.GetMessages(1)}")Use the SeverityLevel class as a function decorator to raise an exception when a tool warning is encountered.
import arcpy
fc1 = 'c:/resources/resources.gdb/boundary'
fc2 = 'c:/resources/resources.gdb/boundary2'
@arcpy.SeverityLevel(1)
def myFunc(fc1, fc2):
print(f"Severity is set to : {arcpy.GetSeverityLevel()}")
try:
# FeatureCompare returns warning messages when a miscompare is
# found. This normally would not cause an exception; however, by
# setting the severity level to 1, all tool warnings will also
# return an exception.
arcpy.management.FeatureCompare(fc1, fc2, "OBJECTID")
except arcpy.ExecuteWarning:
print(f"Warning: {arcpy.GetMessages(1)}")
except arcpy.ExecuteError:
print(f"Error: ", arcpy.GetMessages(2))
myFunc(fc1, fc2)