问题
After using F# option
type for a while, I realize that it could be used for handling exceptional cases. I can use either option
or Exception
in the following examples:
- The
find
functions from List/Array/Seq modules raiseKeyNotFoundException
in uncommon cases, while correspondingtryFind
counterparts returnNone
in those situations. - When I do backtracking (in solving N-queens, Sudoku, etc), whenever a branch has no solution, I can either raise an exception and catch it later or return None to match that value to backtrack. Those cases occur quite often until we find a solution.
My impression is option
is a more functional approach, while Exception
is more commonly-used in the .NET platform.
What are differences between option
and Exception
in exception handling in terms of usability, performance, etc? In which cases using a technique is better than using the other?
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/7952625/option-vs-exception-in-exception-handling