7 people like it.

failwithf for any exception type

Works with F# 4.0 and above

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
let inline raisef (exnCtor : string -> #exn) fmt =
    Printf.kprintf (exnCtor >> raise) fmt

// Before
do raise <| System.ArgumentException(sprintf "Unexpected argument %A" "foo")

// After
do raisef System.ArgumentException "Unexpected argument %A" "foo"
val raisef : exnCtor:(string -> #exn) -> fmt:Printf.StringFormat<'b,'c> -> 'b

Full name: Script.raisef
val exnCtor : (string -> #exn)
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type exn = System.Exception

Full name: Microsoft.FSharp.Core.exn
val fmt : Printf.StringFormat<'b,'c>
module Printf

from Microsoft.FSharp.Core
val kprintf : continutation:(string -> 'Result) -> format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.Printf.kprintf
val raise : exn:System.Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
namespace System
Multiple items
type ArgumentException =
  inherit SystemException
  new : unit -> ArgumentException + 4 overloads
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Message : string
  member ParamName : string

Full name: System.ArgumentException

--------------------
System.ArgumentException() : unit
System.ArgumentException(message: string) : unit
System.ArgumentException(message: string, innerException: exn) : unit
System.ArgumentException(message: string, paramName: string) : unit
System.ArgumentException(message: string, paramName: string, innerException: exn) : unit
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Raw view Test code New version

More information

Link:http://fssnip.net/7R7
Posted:7 years ago
Author:Eirik Tsarpalis
Tags: failwith