Catch

Specifies one or more statements to execute if a value or error is thrown during execution of a Try statement.

Catch ErrorClass as OutputVar
{
    Statements
}

パラメータ

ErrorClass

型:Class

ErrorTimeoutErrorMyCustomErrorなど、捕捉すべき値のクラスです。これは、カンマで区切られたクラスのリストであることも可能です。各クラスのプロトタイプはロード時に解決されるため、クラスは任意の表現ではなく、正確なフルネームで指定する必要があります。Errorから派生していなくても、組み込みクラスやユーザー定義クラスであれば使用可能です。

クラスが指定されていない場合、デフォルトはErrorです。

To catch anything at all, use catch Any.

無効なクラス名を使用した場合や、同名のローカル変数が存在するためにクラスにアクセスできない場合、ロードタイムエラーが表示されます。

OutputVar

型:変数

投げられた値を格納する出力変数で、通常はErrorオブジェクトです。これは動的な変数ではありえない。

省略された場合、投げられた値に直接アクセスすることはできませんが、パラメータなしでThrowを使用することにより、再投げることができます。

値またはエラーがスローされた場合に実行するです。

一般に、単一のステートメントのみを使用する場合は、中括弧は不要です。詳しくは、{...} (block)を参照してください。

備考

Multiple Catch statements can be used one after the other, with each one specifying a different class (or multiple classes). If the value is not an instance of any of the listed classes, it is not caught by this Try-Catch, but might be caught by one further up the call stack.

Every use of Catch must belong to (be associated with) a Try statement above it. A Catch always belongs to the nearest unclaimed Try statement above it unless a block is used to change that behavior.

The parameter list may optionally be enclosed in parentheses, in which case the space or tab after catch is optional.

Catch may optionally be followed by Else, which is executed if no exception was thrown within the associated Try block.

OTB(One True Brace)スタイルはオプションで使用することができます。事例:

try {
    ...
} catch Error {
    ...
}

ロードタイムエラーはtry文が実行される前に発生するため、捕捉することができません。

Try, Throw, Error Object, Else, Finally, Blocks, OnError

Tryをご覧ください。