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?

Throw

Try ブロックから Catch 例外ハンドラへ例外をスローします。

Synopsis

Throw [oref]

引数

oref オプション — ユーザ定義のオブジェクト参照。

説明

Throw コマンドは、Try コマンドにより定義されたコード・ブロック内から、明示的に例外を発行します。Throw を発行すると、Try ブロックから対応する Catch 例外ハンドラへ実行が転送されます。

Throw は、明示的な例外を発行するために使用されます。Caché は、実行時エラーの発生時に暗黙的な例外を発行します。実行時エラーにより、Catch 例外ハンドラへスローする例外オブジェクトが生成されます。

Throw には、以下の 2 つの形式があります。

  • 引数なし

  • 引数付き

引数なしの Throw

引数なしの Throw は、エラー処理を対応する Catch エラー・ハンドラに転送します。スタックにプッシュされるオブジェクトはありませんが、%New() メソッドが呼び出されます。

引数付きの Throw

Throw oref はユーザ定義のオブジェクト参照を指定します。このオブジェクト参照を Catch コマンドにスローして、実行スタックにプッシュします。%New() メソッドの呼び出しはオプションです。

引数

expression

ユーザ定義のオブジェクト参照 (oref)。例えば、Throw ##class(%Exception).%New()。この例外オブジェクトを作成してデータを入れるのは、プログラマの責任です。

以下は、引数を付けずに Throw を使用する例です。

  Try
    SET x=2
    PRINTLN "about to divide by ",x
  Throw
    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"

関連項目

  • Catch コマンド

  • Try コマンド

  • Err オブジェクト

FeedbackOpens in a new tab