处理已知异常

异常是指中断机器人运行时的意外事件。 虽然是意外事件,但开发者可以预测这些事件。 此部分描述了如何处理 IBM RPA 脚本中的已知异常。

可以通过以下方式来处理已知异常:

  • 使用 Handle Error (onError) 命令处理异常事件。
  • 使用流程控制条件命令进行验证。

例如,请考虑以下脚本:unhandled_division。 您可以复制以下脚本并将其粘贴到 IBM RPA Studio 的脚本选项卡中。

defVar --name text_dividend --type String
defVar --name text_divisor --type String
defVar --name num_dividend --type Numeric
defVar --name num_divisor --type Numeric
defVar --name quotient --type Numeric
// Prompt user for dividend and divisor. 
// Perform division of dividend by divisor and show quotient.

inputBox --title "The dividend" --prompt "Enter the dividend" text_dividend=value
convertStringToNumber --text "${text_dividend}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_dividend=value
inputBox --title "The divisor" --prompt "Enter the divisor" text_divisor=value
convertStringToNumber --text "${text_divisor}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_divisor=value
evaluate --expression "${num_dividend} / ${num_divisor}" quotient=value
messageBox --title "The quotient" --text "${num_dividend} / ${num_divisor} = ${quotient}" --icon "Information" --buttons "OK" --defaultbutton "FirstButton"

unhandled_division 脚本依赖于用户输入来执行除法并显示结果。 如果机器人不验证输入,那么依赖原始用户输入可能会变得很麻烦。

将文本转换为数字 (convertStringToNumber) 命令将文本变量转换为数字变量。 此命令假定文本变量具有期望模式,即需要的是数字。 但是, Input Box (inputBox) 命令接受来自用户的任何文本输入。 在 unhandled_division 中,如果用户在输入对话框窗口中输入非数字内容,那么将发生运行时异常。

我们可以将输入错误输入的事件分类为已知异常,因为它会中断机器人的运行时,但我们可以正确对其进行处理。 可以通过以下方式来处理此异常:

  • 捕获异常
  • 验证用户输入

处理异常

unhandled_division 脚本中,可以处理像在以下脚本中一样处理异常:

defVar --name text_dividend --type String
defVar --name text_divisor --type String
defVar --name num_dividend --type Numeric
defVar --name num_divisor --type Numeric
defVar --name quotient --type Numeric
defVar --name dividend_acquired --type Boolean
defVar --name divisor_acquired --type Boolean
// Perform division of dividend by divisor and show quotient.

goSub --label get_user_input_for_dividend
goSub --label get_user_input_for_divisor
goSub --label evaluate
messageBox --title "The quotient" --text "${num_dividend} / ${num_divisor} = ${quotient}" --icon "Information" --buttons "OK" --defaultbutton "FirstButton"
beginSub --name get_user_input_for_dividend
// Prompts user for dividend input until acquired successfuly or user aborts.
	while --left "${dividend_acquired}" --operator "Is_True" --negate
		inputBox --title "The dividend" --prompt "Enter the dividend (type abort to stop the bot)" text_dividend=value
		setVar --name "${dividend_acquired}" --value True
		onError --label get_user_input_for_dividend_exception_handler
		convertStringToNumber --text "${text_dividend}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_dividend=value
	endWhile
endSub
beginSub --name get_user_input_for_divisor
// Prompts user for divisor input until acquired successfuly or user aborts.
	while --left "${divisor_acquired}" --operator "Is_True" --negate
		inputBox --title "The divisor" --prompt "Enter the divisor (type abort to stop the bot)" text_divisor=value
		setVar --name "${divisor_acquired}" --value True
		onError --label get_user_input_for_divisor_exception_handler
		convertStringToNumber --text "${text_divisor}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_divisor=value
	endWhile
endSub
beginSub --name get_user_input_for_dividend_exception_handler
// Exception handler subroutine for the get_user_input_for_dividend subroutine.
// Abort if user requests or warn user about invalid input.
	if --left "${text_dividend}" --operator "Matches" --right "(?i)^abort"
		logMessage --message "User wants to abort." --type "Info"
		stopExecution --current
	else
		messageBox --title "Invalid input" --text "Please, enter a valid number!" --icon "Error" --buttons "OK" --defaultbutton "FirstButton"
		setVar --name "${dividend_acquired}" --value False
		recover
	endIf
endSub
beginSub --name get_user_input_for_divisor_exception_handler
// Exception handler subroutine for the get_user_input_for_divisor subroutine.
// Abort if user requests or warn user about invalid input.
	if --left "${text_divisor}" --operator "Matches" --right "(?i)^abort"
		logMessage --message "User wants to abort." --type "Info"
		stopExecution --current
	else
		messageBox --title "Invalid input" --text "Please, enter a valid number!" --icon "Error" --buttons "OK" --defaultbutton "FirstButton"
		setVar --name "${divisor_acquired}" --value False
		recover
	endIf
endSub
beginSub --name evaluate
// Evaluate division. onError watches for unexpected behaviour.
	onError --label evaluate_exception_handler
	evaluate --expression "${num_dividend} / ${num_divisor}" quotient=value
endSub
beginSub --name evaluate_exception_handler
// Display error message and stop.
	messageBox --title "Division went wrong!" --text "${wdg:error.Message}" --icon "Error" --buttons "OK" --defaultbutton "FirstButton"
	stopExecution --current
endSub

可以在特定子例程上使用处理错误 (onError) 来处理该作用域内的已知错误。 另一种可能的做法是验证用户输入:

defVar --name text_dividend --type String
defVar --name text_divisor --type String
defVar --name num_dividend --type Numeric
defVar --name num_divisor --type Numeric
defVar --name quotient --type Numeric
defVar --name valid_input --type Boolean
defVar --name user_input --type String
// Perform division of dividend by divisor and show quotient.

goSub --label get_user_input_for_dividend
goSub --label get_user_input_for_divisor
evaluate --expression "${num_dividend} / ${num_divisor}" quotient=value
messageBox --title "The quotient" --text "${num_dividend} / ${num_divisor} = ${quotient}" --icon "Information" --buttons "OK" --defaultbutton "FirstButton"
beginSub --name get_user_input_for_dividend
// Prompt user for dividend until valid input or user wants to abort.
	setVar --name "${valid_input}" --value False
	while --left "${valid_input}" --operator "Is_True" --negate
		inputBox --title "The dividend" --prompt "Enter the dividend (use . for decimals). This ignores commas. Type abort to end bot." text_dividend=value
		replaceText --texttoparse "${text_dividend}" --textpattern "," text_dividend=value
		if --left "${text_dividend}" --operator "Matches" --right "(?i)^abort"
			goSub --label abort_execution
		else
			goSub --label validate_user_input --assignments "${user_input}=${text_dividend}"
			gosubIf --label warn_invalid_input --left "${valid_input}" --operator "Is_True" --negate
		endIf
	endWhile
	convertStringToNumber --text "${text_dividend}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_dividend=value
endSub
beginSub --name get_user_input_for_divisor
// Prompt user for divisor until valid input or user wants to abort.
	setVar --name "${valid_input}" --value False
	while --left "${valid_input}" --operator "Is_True" --negate
		inputBox --title "The divisor" --prompt "Enter the divisor (use . for decimals).  This ignores commas. Type abort to end bot." text_divisor=value
		replaceText --texttoparse "${text_divisor}" --textpattern "," text_divisor=value
		if --left "${text_divisor}" --operator "Matches" --right "(?i)^abort"
			goSub --label abort_execution
		else
			goSub --label validate_user_input --assignments "${user_input}=${text_divisor}"
			gosubIf --label warn_invalid_input --left "${valid_input}" --operator "Is_True" --negate
		endIf
	endWhile
	convertStringToNumber --text "${text_divisor}" --allowleadingsign  --allowdecimalpoint  --allowthousands  num_divisor=value
	if --left "${num_divisor}" --operator "Equal_To" --right 0
	// Handle when divisor is 0.
		messageBox --title "Invalid input" --text "Divisor can/'t be 0." --icon "Warning" --buttons "OK" --defaultbutton "FirstButton"
		goSub --label get_user_input_for_divisor
	endIf
endSub
beginSub --name validate_user_input
// Validade user input.
// User input must be a valid number with decimals separated by point.
	replaceText --texttoparse "${user_input}" --textpattern "," user_input=value
	isMatch --text "${user_input}" --regexPattern "^//d+(?:.?//d+)?$" --regexOptions "0" valid_input=value
endSub
beginSub --name abort_execution
// Log and abort execution.
	logMessage --message "User wants to abort." --type "Info"
	stopExecution --current
endSub
beginSub --name warn_invalid_input
// Warn when input is invalid.
	messageBox --title "Invalid input" --text "Please, enter a valid number (use . for decimals)." --icon "Warning" --buttons "OK" --defaultbutton "FirstButton"
endSub

可以使用流程控制条件来验证已知异常。 通过此类型的验证,您可以更轻松地创建特定异常处理逻辑,但如果有许多可能的异常事件,这可能会使脚本大小增大。

success 输出参数

IBM Robotic Process Automation 中的某些命令具有名为 success 的输出参数。 此输出参数返回布尔值,表示命令是否成功执行了其任务。 您可以通过创建备选条件流来验证此输出参数,以确定是否处理了脚本中的已知异常事件。