F # exception and internal exception property

in F #, I can declare a custom exception, for example: exception Foo of string ,

which maps the string to the Message property.

how can I declare one that I can use later to create a catch exception as an internal exception to this? In other words, how do I do something like (pseudo)

 try ... with e -> raise Foo (message, innerException) where innerException is e? 

Thanks!

+4
source share
1 answer

The simple declaration of exceptions using exception limited in many ways. If you want to use other functions of the standard .NET exceptions (i.e., an internal exception), you can declare the exception as a class:

 open System type FooException(message:string, innerException:Exception) = inherit Exception(message, innerException) 

You can also provide overloaded constructors, for example, if you want to use null as the default for InnerException . An exception can be raised as a regular .NET exception using raise :

 raise (new FooException(message, e)) 
+8
source

Source: https://habr.com/ru/post/1383711/


All Articles