Skip to content

Instantly share code, notes, and snippets.

@Szer
Created February 25, 2020 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/cf5caa7e3b2a047c811701561e7026f3 to your computer and use it in GitHub Desktop.
Save Szer/cf5caa7e3b2a047c811701561e7026f3 to your computer and use it in GitHub Desktop.
ExceptionHandling
open System
let (|Is|_|) (x: 'b) : 'a option =
match box x with
| :? 'a as a -> Some a
| _ -> None
let (|Inner|_|) (b: exn): 'a option when 'a :> Exception =
match box b.InnerException with
| :? 'a as a -> Some a
| _ -> None
try
()
with
| :? AggregateException as a when (a.InnerException :? NullReferenceException) ->
let b = a.InnerException :?> NullReferenceException
printfn "%s" b.Message
try
let a = new AggregateException(new NullReferenceException("MOCK"))
raise a
with
| Is (a: AggregateException) & Inner (b: NullReferenceException) ->
printfn "%s" b.Message // b is NullReferenceException here, no need to cast
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment