Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# exception and inner exception property

Tags:

f#

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!

like image 943
Alex Avatar asked Nov 29 '11 19:11

Alex


1 Answers

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))
like image 121
Tomas Petricek Avatar answered Nov 06 '22 17:11

Tomas Petricek