# Exceptions vs Option

**URL:** <https://discuss.ocaml.org/t/exceptions-vs-option/1559>\
**Category:** Learning\
**Tags:** ocaml\
**Created:** [February 12, 2018, 6:25am UTC](https://discuss.ocaml.org/t/exceptions-vs-option/1559 "2018-02-12T06:25:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dm01](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@dm01](https://discuss.ocaml.org/u/dm01)\
**Post date:** [February 12, 2018, 6:25am UTC](https://discuss.ocaml.org/t/exceptions-vs-option/1559/1 "2018-02-12T06:25:33Z")

</div>

Hi,

What of methods is more preferable:

match f () with  
| Some x -\> …  
| None -\> …

or

match f () with  
| x -\> …  
| exception Not\_found -\> …

And in general. If there will be algebraic effects in OCaml whether we have to stop using monads? Ocaml allows to write a code in any style,  
but it seems to me that it is good to have the conventional standards.

---

<div class="post-metadata">

**Author:** ![bobbypriambodo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/bobbypriambodo/32/179_2.png) [@bobbypriambodo](https://discuss.ocaml.org/u/bobbypriambodo)\
**Post date:** [February 12, 2018, 10:46am UTC](https://discuss.ocaml.org/t/exceptions-vs-option/1559/2 "2018-02-12T10:46:45Z")

</div>

Matching on exceptions means you are giving up the help of the compiler to do exhaustiveness check. In the first one, if you don’t add `| None ->` clause, the compiler will complain that your match clauses are not exhaustive. AFAIK the second one will not if you missed the `| exception Not_found ->` clause.

I will not speak for conventional standards, since I’m not sure whether there’s any, but in general I think it is desirable to strive for using `option` in this case.

In a more general case, for modeling an _operation that may fail_, it’s better to use `result` instead of `option`, and only use `option` if the data strictly either exists or does not exist.

What’s more, using monads will make it easier to chain operations later on (via `map`, `bind`, etc.)

---

<div class="post-metadata">

**Author:** ![didier-wenzek](https://avatars.discourse-cdn.com/v4/letter/d/9de0a6/32.png) [@didier-wenzek](https://discuss.ocaml.org/u/didier-wenzek)\
**Post date:** [February 13, 2018, 9:39am UTC](https://discuss.ocaml.org/t/exceptions-vs-option/1559/3 "2018-02-13T09:39:38Z")

</div>

This recent post on [`r/ocaml`](https://www.reddit.com/r/ocaml/comments/7x4m0e/composable_error_handling_in_ocaml/) explores 4 ways to handle errors in OCaml. An interesting reading.

---

<div class="post-metadata">

**Author:** ![dm01](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@dm01](https://discuss.ocaml.org/u/dm01)\
**Post date:** [February 13, 2018, 11:30am UTC](https://discuss.ocaml.org/t/exceptions-vs-option/1559/4 "2018-02-13T11:30:20Z")

</div>

Thanks a lot for very detailed and clear answer.

Possibly, the second part of my question is too abstract.  
And I understand that hardly anyone that will be able to tell something certain now.

I meant what I don’t understand as monads and effects will coexist together.  
(IMHO, Option is something from monadic world).  
I.e. if effects will be accepted in OCaml, then, perhaps,  
something like following constructions will be used:

match f() with  
| x -\> …  
| effect NotFound k -\> …

And, looking in the future whether we have to adapt to new reality gradually?
