开发者

How to throw exceptions with meaningful messages in F#?

How to throw meaningful exceptions in F# and catch them in C#?

With the following code:

F# Library:

module Test

exception TestExc of string

let throwit message : unit=
    raise (TestExc("custom exception with message: " + message))

C# Client:

namespace TestExc
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Test.throwit("Hi there");
            }
            ca开发者_高级运维tch (Test.TestExc te)
            {
                Console.WriteLine("Caught exception with message: " + te.Message);
            }
        }
    }
}

I get the following message:

Caught exception with message: Exception of type 'Test+TestExc' was thrown.

But I want to see Hi there as the caught exception message.

What is the F# equivalent of the following?

class CSTestExc:System.Exception 
{
        public CSTestExc(String message) : base(message) { }
}


Rather than creating exception using exception TestExc of string create exception as :

type MyExp (msg:string) = 
        inherit Exception(msg)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜