当 Rational ClearQuest API 中的例程遇到意外情况时,它们会抛出异常。如果异常未由调用程序捕获,那么语言解释器就会终止您的程序。如果异常有可能造成 Rational ClearQuest API 调用失败,您就应当捕获并处理异常。
eval {enter statements you want to monitor};
eval{$objectName->MethodName();}; if ($@) { print "Error using MethodName method. Error: $@\n"; } else { # continue without error ... }
某些预期通常失败的函数不包含在此范围内。尤其是验证和设置字段函数返回错误说明,而不是抛出异常。 要了解更多信息,请参阅“错误检查和验证”。
错误检查
对于 Rational ClearQuest API 的许多方法和属性,您必须检查返回值以验证调用是否返回错误。
if (!defined($result)) { ... };来检测这种情况。
对于具有错误字符串返回值的函数的调用,如果未发生错误,那么该值为空,否则会返回包含错误描述的字符串。您可以检查调用方法的结果,如果值不为空,那么您可以检索变量中的错误(字符串值)。
例如,可将 Entity 对象的 SetFieldValue 方法定义为返回一个字符串值。如果允许对字段进行更改并且操作成功,那么会返回空的字符串;否则,如果操作失败,该方法会返回一个包含错误说明的字符串。
The Defect SAMPL00000123 does not have a field named "Invalid_field".
# trap exceptions and error message strings # ... eval { $RetVal = ${$CQEntity}->Validate(); }; # EXCEPTION information is in $@ # RetVal is either an empty string or contains a failure message string if ($@){ print "Exception: '$@’\n"; # other exception handling goes here... } if ($RetVal eq ""){ # success... } else { # failure... # return the message string here... }