例外の種類Exception Types
F# の例外の 2 つのカテゴリがあります: .NET 例外の種類と F# の例外の種類。There are two categories of exceptions in F#: .NET exception types and F# exception types. このトピックでは、定義して、F# の例外の種類を使用する方法について説明します。This topic describes how to define and use F# exception types.
構文Syntax
exception exception-type of argument-type
RemarksRemarks
前の構文で例外型新しい F# の例外型の名前を指定および引数の型この種類の例外が発生するときに指定できる引数の型を表します。In the previous syntax, exception-type is the name of a new F# exception type, and argument-type represents the type of an argument that can be supplied when you raise an exception of this type. 引数の型にタプル型を使用すると、複数の引数を指定できます。You can specify multiple arguments by using a tuple type for argument-type.
F#例外の一般的な定義は、次のようになります。A typical definition for an F# exception resembles the following.
exception MyError of string
この型の例外を生成するには、次raise
のように関数を使用します。You can generate an exception of this type by using the raise
function, as follows.
raise (MyError("Error message"))
次の例にF#示すように、 try...with
式のフィルターでは、例外の種類を直接使用できます。You can use an F# exception type directly in the filters in a try...with
expression, as shown in the following example.
exception Error1 of string
// Using a tuple type as the argument type.
exception Error2 of string * int
let function1 x y =
try
if x = y then raise (Error1("x"))
else raise (Error2("x", 10))
with
| Error1(str) -> printfn "Error1 %s" str
| Error2(str, i) -> printfn "Error2 %s %d" str i
function1 10 10
function1 9 2
例外の種類で定義された、 exception
F# でのキーワードはから継承する新しい型System.Exception
します。The exception type that you define with the exception
keyword in F# is a new type that inherits from System.Exception
.
関連項目See also
フィードバック
フィードバックを読み込んでいます...