Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Exception Types (F#)

There are two categories of exceptions in F#: .NET exception types and F# exception types. This topic describes how to define and use F# exception types.

exception exception-type of argument-type

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.

A typical definition for an F# exception resembles the following.

exception MyError of string

You can generate an exception of this type by using the raise function, as follows.

raise (MyError("Error message"))

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

The exception type that you define with the exception keyword in F# is a new type that inherits from Exception.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft