in F# i can declare a custom exception like so:
exception Foo of string
,
which will map the string to the Message property.
how do I declare one that I can later use to raise a caught exception as the inner exception of this one? In other words, how do i do something like (pseudo)
try
...
with e -> raise Foo (message, innerException) where innerException is e?
Thanks!
The simple declaration of exceptions using exception
is limited in many ways. If you want to use other features of standard .NET exceptions (i.e. inner exception), then you can declare 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 a default value for InnerException
.
The exception can be raised as an ordinary .NET exception using raise
:
raise (new FooException(message, e))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With