SetSeverityLevel

摘要

用于控制地理处理工具抛出异常的方式。

说明

如果未使用 SetSeverityLevel 函数,则默认行为相当于将 severity_level 参数设置为 2;即,仅当工具出现错误时,工具才会抛出异常。

语法

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

Specifies 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.management.FeatureCompare(fc1, fc2, "OBJECTID")
except arcpy.ExecuteWarning:
    print(arcpy.GetMessages(1))
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

相关主题