generics - 如何在F#中声明通用异常类型

标签 generics exception f#

如何定义如下异常?

exception CustomExn<'TMessage> of 'TMessage list

最佳答案

也许您可以仅继承System.Exception?

type CustomExn<'TMessage> (message:'TMessage list) =
    inherit System.Exception ()  

let test =
    try
        raise (CustomExn["string"] )
    with 
    | :? CustomExn<string> -> "CustomExn of string"
    | :? CustomExn<int> -> "CustomExn of int"
    | _ ->  "Everything else"

关于generics - 如何在F#中声明通用异常类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974736/

相关文章:

c# - 将 DataTable 转换为通用列表?

Java 泛型上限通配符限制

python - 使用python捕获异常时如何引发DeprecationWarning?

c# - 在类库级别捕获 C# 错误

f# - 错误 FS0001 : The type 'string -> seq<unit>' is not compatible with the type 'seq<' a>'

function - 使用 F# 的“选项<unit->unit> 类型和等式”

java - 仅在 Maven 上泛型编译错误

java - 获取 XML 包中返回的 SQLException 消息?

F# - 公共(public)文字

Java 使用 -Xlint :unchecked for details 重新编译