SetSeverityLevel

描述

用于控制地理处理工具如何抛出异常。

讨论

如果未使用 SetSeverityLevel,默认行为等效于将 severity_level 设置为 2;即,仅当工具出错时,才会抛出异常。

语法

SetSeverityLevel (severity_level)
参数说明数据类型
severity_level

The severity level

  • 0即使工具生成错误或警告,也不会抛出异常。
  • 1如果工具生成警告或错误,将抛出异常。
  • 2如果工具生成错误,将抛出异常。这是默认设置。
Integer

代码示例

SetSeverityLevel 示例

使用 SetSeverityLevel 可强制工具在遇到工具警告时抛出异常。

import arcpy

fc1 = 'c:/resources/resources.gdb/boundary'
fc2 = 'c:/resources/resources.gdb/boundary2'

# Set the severity level to 1 (tool warnings will throw an exception)
arcpy.SetSeverityLevel(1)
print("Severity is set to : {0}".format(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.FeatureCompare_management(fc1, fc2, "OBJECTID")
except arcpy.ExecuteWarning:
    print(arcpy.GetMessages(1))
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

相关主题