LOTUSSCRIPT 言語
ゼロによる除算エラーを生成する可能性のある Print ステートメントをスクリプトに含めます。ゼロによる除算エラーを処理するために、On Error ステートメントをスクリプトに入れます。このステートメントは、ゼロによる除算エラーを指定して適切に対処するエラー処理ルーチンを示します。このルーチンは DivZero ラベルで開始されます。ルーチンには InputBox$ 関数呼び出しが含まれています。これは開いているファイルから読み込まれたゼロに代わる値を入力するように、ユーザーにプロンプトを表示します。追加の On Error ステートメントは次のとおりです。
On Error ErrDivisionByZero GoTo DivZero
エラー処理ルーチンは次のようになります。
DivZero: number3% = InputBox$("Number3 is 0. Enter a new value:") ' Resume execution with the statement that caused ' the error ErrDivisionByZero. Resume
他のエラーが発生しても、スクリプトの実行を終了させずにすべて確実に処理されるように、エラーを特定しない On Error ステートメントをスクリプトに入れます。
次の例では、ファイルオープンエラーとゼロによる除算エラーを特定して管理するスクリプトを示します。他のエラーはすべて汎用の On Error ステートメントに含まれます。
%Include "LSERR.LSS" Sub GetLine Dim number1 As Integer, number2 As Integer, number3 _ As Integer Dim fileName As String ' The error-handling routine at label Leave is for ' all errors except the two individual errors ' specified in the second and third On Error statements. ' Each has a specific error-handling routine designated. On Error GoTo Leave On Error ErrOpenFailed GoTo NoExist On Error ErrDivisionByZero GoTo DivZero GetName: fileName$ = InputBox$("Enter a file name:") Open fileName$ For Input As #1 Input #1, number1%, number2%, number3% Print number1%, number2%, number3%
' The next statement causes a division-by-zero error if ' number 3 is 0. Print (number1% + number2%) / number3% Close #1 Exit Sub NoExist: Print Error(), Err(), Erl() Resume GetName DivZero: number3% = InputBox("Number3 is 0. Enter a new value:") Resume Leave: ' The following message is general, because different ' errors may have occurred. MessageBox("Cannot complete operation.") Exit Sub End Sub
GetLine を呼び出す次の例は、サブルーチンがどのように機能するかを示します。ファイルオープンエラーとゼロによる除算エラー以外のエラーの場合は、ラベル Leave にあるエラー処理ルーチンがメッセージボックスを表示し、GetLine サブルーチンから戻ります。
Call GetLine() ' The user enters a valid file name, and the values read in ' from the file are 11, 22, and 0. ' Output: ' 11 22 0 ' The value 0 causes a division-by-zero error. ' The user then enters the value 2 into the input box ' specified in the error-handling routine beginning at ' DivZero.Execution resumes at the Print statement that ' generated the error. 'Output: 16.5
しかし、DivZero のエラー処理ルーチンの入力ボックスに、ユーザーが 2 の代わりに 99999 を入れると、結果はオーバーフロー (桁あふれ) エラーになります。これは、99999 は変数 number3% の最大有効整数値より大きいからです。このエラーは、DivZero のエラー処理ルーチンの中で発生しているため、処理されません。エラー処理ルーチン内でエラーが発生すると、LotusScript が実行を終了します。
On Error ステートメントの順序任意のエラーに対して、ある一時点で効力のあるエラー処理ルーチン (またはルーチンなし) は、そのエラーに適用される On Error ステートメントで最後に実行された On Error ステートメントに指定されているルーチンです。On Error ステートメントの順序を変更すると、実行時の処理が変更される可能性があります。 次の例では、上の例の最初に指定されている 3 つの On Error ステートメントの順序が変更されています。
' Two routines are designated to handle individual errors. On Error ErrOpenFailed GoTo NoExist On Error ErrDivisionByZero GoTo DivZero ' The Leave routine is intended to handle all other errors. On Error GoTo Leave
これらの 3 つのステートメントの実行後、エラーはすべてラベル Leave で開始するエラー処理ルーチンで処理されます。これは、ステートメント On Error GoTo Leave がすべてのエラーを指すからです。Leave というルーチンが、その前の 2 つの On Error ステートメントで指定されている ErrOpenFailed と ErrDivisionByZero 用に設定されているルーチンを上書きします。
関連項目