The CLR makes the operation of throwing and catching an exception extremely expensive. For this reason alone, you should prefer constructs like Option for reporting expected failures. If the failure is truly exceptional and nearly unrecoverable, go ahead and throw an exception.
But as you note, things like backtracking during a search are unexceptional, and you will find your performance suffers greatly if you implement them with exceptions Because this is a property of the CLR, it doesn't really matter whether you are in F# or not. My understanding is that other runtimes for ML-like languages, e.g. Ocaml, do not have this characteristic, and so may use exceptions more often for control flow.
The CLR makes the operation of throwing and catching an exception extremely expensive. For this reason alone, you should prefer constructs like Option for reporting expected failures. If the failure is truly exceptional and nearly unrecoverable, go ahead and throw an exception.
But as you note, things like backtracking during a search are unexceptional, and you will find your performance suffers greatly if you implement them with exceptions. Because this is a property of the CLR, it doesn't really matter whether you are in F# or not. My understanding is that other runtimes for ML-like languages, e.g. Ocaml, do not have this characteristic, and so may use exceptions more often for control flow.
Indeed, I once measured this and found that C++ exceptions are 6x slower than OCaml and . NET's are 600x slower than OCaml! – Jon Harrop Nov 1 at 18:33.
My question is what are differences between Option and Exception in exception handling in terms of usability, performance...? 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. In which cases using a technique is better than the other?
Whenever I'm writing code like servers and daemons that needs to keep running I catch as many exceptions as possible and replace them with values of union types like option. The static type system then forces me 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.
From the theoretical point of view. Option is something that you should use in those functions which are pure from FP point of view (you can google about what are pure functions). Exceptions are more about the impure world (Like the IO world in Haskell).
Now from practicably point of view I would suggest using option as return type when the logic of you application says that the value can be there or it cannot be i. E the value not being present is a valid application rule. Exceptions are something which should be raised when something happens in the application logic which indicates the incorrect logic execution or some incorrect application state which was is not expected as par the application rules.
From performance POV throwing exception is more expensive (due to stack unwinding - looking for the appropriate exception handler - etc) as compared to returning option types.
I don't agree with the comment about performance. Option types are more lightweight than Exception types (see @Sebastian Good's answer). – pad Oct 31 at 12:24 My text says exception which will bad .. that means exceptions are not good in performance – Ankur Oct 31 at 12:27 Sorry, my mistake for not reading carefully :) – pad Oct 31 at 12:31 +1; Yet, you may want to rephrase your last sentence as I, too, had trouble interpreting it correctly.
– Novox Oct 31 at 15:13 Updated the last sentence about performance, I hope it is much clear now than the earlier one with strange recursive look :) – Ankur Oct 31 at 16:45.
In terms of usability I prefer options in F#. Options encapsulate the exceptional state as well as the expected state. This allows you to deffer handling of the exceptional state until you need the expected state.
Option also has a number of helper functions you'd have to write yourself with exceptions. Partial Active Patterns allow you to handle multiple exceptional cases very cleanly with options. Optional Paramaters in F# are implemented with Options.
This deferred nature I mentioned above allows you to not care what generated the exceptional case, so long as the consumer has a default value for it. Options are a fundamentally different way of thinking about exceptional cases and I believe help to make F# special.
1 for a very thoughtful answer. That's what I want to know about using option in exception handling. – pad Oct 31 at 22:51.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.