エラーを処理

スクリプトの実行時に、次のコマンドに進む際やサブルーチンを起動する際に発生する可能性のある例外を処理します。

コマンドの可用性: IBM RPA SaaS および IBM RPA オンプレミス

説明

例外とは予期しないイベントであり、ボットが実稼働環境やデバッグ・モードで実行されるときにエラーが発生する原因となる可能性があります。 例外の処理方法について詳しくは、例外処理を参照してください。 デバッグ・モードで例外を処理する場合は、スクリプトのデバッグおよび開始オプションも参照してください。

スクリプト構文

IBM RPA の専有スクリプト言語の構文は、他のプログラミング言語と似ています。 スクリプト構文は、スクリプト・ファイル内のコマンドの構文を定義します。 この構文は、 IBM RPA Studioの 「スクリプト」 モードで操作できます。

onError [--executenext(Boolean)] --label(String)

入力パラメーター

以下の表は、このコマンドで使用可能な入力パラメーターのリストを示しています。 この表では、 IBM RPA Studioのスクリプト・モードで作業しているときのパラメーター名と、それに相当する Designer モードのラベルを確認できます。

「デザイナー」モードのラベル 「スクリプト」モードの名前 必須 使用可能な変数タイプ 説明
次を実行 executenext Optional Boolean これを有効にすると、スクリプトの実行中にエラーが発生した場合に、次のコマンドを実行できます。
サブルーチン label Required when the Execute Next parameter is False Text スクリプトの実行時にエラーが発生した場合にトリガーするサブルーチン。

例 1

スクリプトでエラーが発生すると、ランタイムが完全に停止し、ボットが他のタスクを完了できなくなります。 ほとんどの場合、自動化にとって重要ではないいくつかの例外を無視して、完了するまでボットを実行し続けることができます。 以下の例では、このコマンドは、数値をゼロで除算しようとすると発生するエラーを無視し、ボットは他のすべてのコマンドを正常に実行します。

defVar --name firstValue --type Numeric
defVar --name secondValue --type Numeric
defVar --name result --type Numeric
// Handle an expected exception in the script. In this case, it advances to the next command.
onError --executenext
// Assigns values to variables to run a mathematical expression.
setVars --assignments "${firstValue}=2,${secondValue}=0"
// Calculate a mathematical expression based on a value provided before.
evaluate --expression "${firstValue}/${secondValue}" result=value
// Logs the operation's result.
logMessage --message "The operation\'s result is: ${result}" --type "Info"

例 2

ただし、エラー発生後に実行されるコマンドのサブルーチンの後に、特定のアクションでエラーを処理したい場合があります。 この場合、実行時に発生したエラーをユーザーに認識させることができます。また、エラーからリカバリーするために、スクリプト内の result 変数にデフォルト値を割り当てることもできます。

以下の例では、サブルーチンを使用してエラーからリカバリーします。 IBM RPA Studio デバッグ・モードでスクリプトを実行すると、引き続きエラー・メッセージ・ボックスが表示されますが、エラー発生後もスクリプトの実行を続行できます。

defVar --name firstValue --type Numeric
defVar --name secondValue --type Numeric
defVar --name result --type Numeric
// Handle an expected exception in the script. In this case, it advances to the next command.
onError --label evaluateException
// Assigns values to variables to run a mathematical expression.
setVars --assignments "${firstValue}=2,${secondValue}=0"
//Calculate a mathematical expression based on a value provided before.
evaluate --expression "${firstValue}/${secondValue}" result=value
//Logs the operation's result.
logMessage --message "The operation\'s result is: ${result}" --type "Info"
beginSub --name evaluateException
// Outputs a message indicating that the division went wrong.
	logMessage --message "Something went wrong: ${rpa:errorMessage}" --type "Info"
// Assigns 0 to the result variable	
  setVar --name "${result}" --value 0
// Recovers the script runtime, running the next command.
	recover
endSub