Section 1.6 "exceptions" of ocaml manual

Original code:

# let name_of_binary_digit digit =
    try
      List.assoc digit [0, "zero"; 1, "one"]
    with Not_found ->
      "not a binary digit";;

Code adapted to JS/core

  let name_of_binary_digit digit =
    try List.Assoc.find_exn [ (0, "zero"); (1, "one") ] digit ~equal:( = ) with Not_found -> "not a binary digit"

Error I get:

247 |     try List.Assoc.find_exn [ (0, "zero"); (1, "one") ] digit ~equal:( = ) with Not_found -> "not a binary digit"
                                                                                      ^^^^^^^^^
Error (alert deprecated): Not_found
[since 2018-02] Instead of raising [Not_found], consider using [raise_s] with an
informative error message.  If code needs to distinguish [Not_found] from other
exceptions, please change it to handle both [Not_found] and [Not_found_s].  Then, instead
of raising [Not_found], raise [Not_found_s] with an informative error message.

My confusion:

I am CATCHING, not THROWING an exception at that location. What is going on, and how do I fix this ?

1 Like

The Core and Base stdlib replacements hide the original Not_found exception and re-export it with a deprecation. Since List is going to come from Core/Base too, you should catch Not_found_s.

P.S. I’m not sure how the warning logic can detect and not warn handling of both Not_found and Not_found_s… Also, maybe Caml.Not_found would make the warning go away? (Untested.)

1 Like

Thanks, this makes sense now.


  let name_of_binary_digit digit =
    try List.Assoc.find_exn [ (0, "zero"); (1, "one") ] digit ~equal:( = ) with Not_found_s _ -> "not a binary digit"

worked.