How to declare a generic exception type in F#

后端 未结 2 1742
后悔当初
后悔当初 2021-01-12 01:52

How can I define an exception like the following?

exception CustomExn<\'TMessage> of \'TMessage list
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-12 02:10

    Maybe you can just inherit from System.Exception?

    type CustomExn<'TMessage> (message:'TMessage list) =
        inherit System.Exception ()  
    
    let test =
        try
            raise (CustomExn["string"] )
        with 
        | :? CustomExn -> "CustomExn of string"
        | :? CustomExn -> "CustomExn of int"
        | _ ->  "Everything else"
    

提交回复
热议问题