Skip to content

Instantly share code, notes, and snippets.

@theburningmonk
Created September 29, 2011 00:03
Show Gist options
  • Select an option

  • Save theburningmonk/1249638 to your computer and use it in GitHub Desktop.

Select an option

Save theburningmonk/1249638 to your computer and use it in GitHub Desktop.
Contrived F# example
open System
let throwException =
function
| 0 -> failwithf "Default exception"
| 1 -> raise <| new ArgumentException("Invalid N")
| 2 -> raise <| new ArgumentNullException()
| 3 | 4 | 5 -> raise <| new ArgumentOutOfRangeException()
| _ -> raise <| new ApplicationException()
let getExitCode =
try
let random = new Random(DateTime.Now.Millisecond)
throwException <| random.Next 8
0
with
// combine patterns
| :? ArgumentNullException
| :? ArgumentOutOfRangeException
-> printfn "Argument was either null or out of range"
1
| :? ArgumentException as argEx
-> printfn "Argument exception was caught: %s" argEx.Message
2
| _ as ex // wildcard match, 'ex' will be of type System.Exception
-> printfn "Generic exception was caught: %s" ex.Message
3
@schuster-rainer

Copy link
Copy Markdown

the wildcard isn't even required.

| ex -> printfn "Generic exception was caught: %s" ex.Message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment