Friday, 4 May 2012

Exceptions vs the F# option type

The option type offers stronger static checking than exceptions, increasing the chances that programmer error will be caught by the compiler. Returning non-exceptionally is likely to be faster than returning Some result but returning exceptionally is hundreds of times slower than returning None.
When writing code like servers and daemons that must keep running, catch as many exceptions as possible and replace them with values of union types like option. The static type system then forces you to handle both exceptional and non-exceptional returns in almost all cases, making it much easier to write code that will not die from an exception propagating unexpectedly.

No comments: