Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

Try

実行中のエラーを監視するコード・ブロックを指定します。

Synopsis

Try
   statements
Catch [exceptionvar]
   statements
End Try

説明

Try コマンドは引数を取りません。Try キーワードと Catch キーワードの間に置かれた 1 つまたは複数の Caché Basic コード文を識別するために使用されます。このコード・ブロックは、構造化された例外処理を実行するために保護されたコードです。このコード・ブロック内で例外が発生した場合、Caché は Err を設定し、Catch コマンドによって識別される例外ハンドラに実行を移します。これは、例外のスローとして知られています。エラーが発生しなかった場合、End Try 文の後にある次の Caché Basic 文から実行が継続します。

例外は、0 による除算の試みなどの実行時エラーの結果として発生する場合があります。また例外は、Throw コマンドの発行により明示的に伝達されない場合があります。

Catch ブロックのすぐ後に、Try ブロックが続く必要があります。TryCatch のペアは、End Try 文で終了します。

以下の例では、Try コード・ブロックが実行されます。ローカル変数 a を設定しようとします。最初の例では、コードが正常に完了し、Catch はスキップされます。2 つ目の例では、0 による除算を表す Err エラーでコードが失敗し、Catch コマンドに実行が渡されます。

Try が成功した場合 :

  Try 
    SET x=2
    PRINTLN "about to divide by ",x
    SET a=7/x
    PRINTLN "Success: the result is ",a
  Catch myvar 
    PRINTLN "this is the exception handler"
    PRINTLN "Error number: ",Err.Number
    PRINTLN "Error is: ",Err.Description
    PRINTLN "Error code: ",myvar.Code
  End Try
  PRINTLN "this is where the code falls through"

Try が失敗した場合 :

  Try
    SET x=0
    PRINTLN "about to divide by ",x
    SET a=7/x
    PRINTLN "Success: the result is ",a
  Catch myvar 
    PRINTLN "this is the exception handler"
    PRINTLN "Error number: ",Err.Number
    PRINTLN "Error is: ",Err.Description
    PRINTLN "Error code: ",myvar.Code
  End Try
  PRINTLN "this is where the code falls through"

関連項目

FeedbackOpens in a new tab