SeverityLevel

摘要

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

说明

SeverityLevel 类可用作函数修饰符或 with 块中的上下文管理器。 当不使用 SeverityLevel 类时,或者当使用 severity_level 参数值 2 时,工具将仅在工具出错时提出异常。 要控制地理处理工具如何为整个脚本提出异常,请使用 SetSeverityLevel 函数。

语法

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

Specifies the severity level.

  • 0A tool will not raise an exception, even if the tool produces an error or warning.
  • 1 If a tool produces a warning or an error, it will raise an exception.
  • 2 If a tool produces an error, it will raise an exception.

(默认值为 2)

Integer

代码示例

SeverityLevel 示例 1

当遇到工具警告时,使用 SeverityLevel 类作为上下文管理器来提出异常。

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)}")
SeverityLevel 示例 2

当遇到工具警告时,使用 SeverityLevel 类作为函数修饰符来提出异常。

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)