【问题标题】:Tracing cases of failure got追踪失败案例
【发布时间】:2020-06-27 17:24:45
【问题描述】:

我正在尝试从字符变量中填充整数变量。如果发现任何错误,我想显示错误消息并跟踪所有可能的失败案例。

//Defining variable

Define variable char_value as character no-undo initial "kk".

Define variable int_value as integer no-undo.

define variable ix as integer no-undo.

Assign int_value = integer(char_value) no-error.

IF ERROR-STATUS:ERROR OR ERROR-STATUS:NUM-MESSAGES > 0 THEN 
DO:
      
MESSAGE ERROR-STATUS:NUM-MESSAGES 
" errors occurred during conversion." SKIP 
"Do you want to view them?" 
VIEW-AS ALERT-BOX QUESTION BUTTONS YES-NO 

UPDATE view-errs AS LOGICAL.

IF view-errs THEN
      DO ix = 1 TO ERROR-STATUS:NUM-MESSAGES:

        MESSAGE ERROR- 
        STATUS:GET-NUMBER(ix) 
        ERROR-STATUS:GET- 
        MESSAGE(ix).
      END.
END.

有两个条件我想知道。

  1. 我给了多少 char 值,所以没有。的错误返回将超过 1。
  2. 如何跟踪所有可能的失败案例。

【问题讨论】:

  • 您的代码导致:“数字输入 k.(76) 中的字符无效”。这就是 Progress 必须提供的关于为什么该字符串的转换失败的所有信息。在那之后就没有什么可说的了。不评估 char_value 中的其他字符(好或坏)。
  • 这样不行。一旦发现一个无效字符,转换就会停止。不会检测或报告其他错误。

标签: openedge progress-4gl


【解决方案1】:

内置的转换例程不会做您希望它做的事情。因此,您需要在尝试转换之前解析您的输入。像这样的:

function isDigit returns logical ( input d as character ):
  if length( d ) = 1 then
    return ( index( "0123456789", d ) > 0 ).
   else
    return no.
end.


procedure checkInteger:

  define input  parameter integerString as character no-undo.
  define output parameter errorList     as character no-undo.
  define output parameter ok            as logical   no-undo.

  define variable i as integer   no-undo.
  define variable n as integer   no-undo.
  define variable c as character no-undo.

  ok = yes.

  n = length( integerString ).
  do i = 1 to n:
    c = substring( integerString, i, 1 ).
    if i = 1 and c = "-" then next.
    if isDigit( c ) = no then
      do:
        ok = no.
        errorList = errorList + substitute( "The character '&1' at offset &2 is not a valid integer value~n", c, i ).
      end.
  end.

  errorList = trim( errorList, "~n" ).      // remove the trailing newline (if any)

  return.

end.

define variable ok as logical no-undo.
define variable errorList as character no-undo.

run checkInteger( "12x34y56z789", output errorList, output ok ).

if ok = yes then
  message "string is a properly formed integer, go ahead and convert it".
 else
  message
    "string was not correctly formed, do not try to convert it" skip
    errorList
   view-as alert-box information
 .

注意 #1 如果输入包含不可打印的字符,errorList 字符串将按字面意思显示它,并且看起来很有趣。当然,您可以将它们编码为更具可读性。这样做留作练习。或者其他问题。

注意 #2 此代码不会尝试检查字符串值是否适合整数或 int64。这也留作练习。

【讨论】:

    【解决方案2】:

    虽然您可以根据需要使解析变得尽可能复杂,但我会保持简单并确保向用户提供足够的信息,在这种情况下是完整的输入值:

    def var cc as char initial "kk".
    def var ii as int.
    
    ii = integer( cc ).
    
    catch e as progress.lang.error:
      message quoter( cc, "'" ) e:getMessage(1) view-as alert-box.
    end catch.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 2012-08-14
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多