2

I don't anderstand the behaviour of the try ... with ... feature of OCaml.

here is a little sample to reproduce my problem :

let () =
  try int_of_string "4" with
  | Failure -> -1
  | n -> n

I compile with

ocamlc test.ml

Then I get this error:

File "test.ml", line 2, characters 6-23:  
2 |   try int_of_string "4" with  
 
Error: This expression has type int but an expression was expected of type unit

How can I modify my little code sample to make it work?

0

1 Answer 1

4

Your code can be read as

let () = (* I expect an unit result *)
 try int_of_string "4"
    (* if the call to `int_of_string` succeeds returns this result,
       which is an int *)
 with (* otherwise if an exception was raised* *)
 | Failure ->
   (* returns 1 if the raised exception was the `Failure` exception *)
   -1 
 | n -> n (* otherwise returns the exception `n` *)

which fails due to a lot of small paper cuts. The corrected version would be

let n =
  try int_of_string "4" with 
  | Failure _  (* Failure contains a error message as an argument *)  -> - 1

Nevertheless, I would suggest to use the match ... with exception ... -> ... construct which may be closer to your intuition

let n = match int_of_string "4" with
| exception Failure _msg -> -1
| x -> x (* if there was no exception, returns the result *)

but you can also avoid the exception with

let n = match int_of_string_opt "4" with
| None -> -1
| Some x -> x

or even

let n = Option.value ~default:(-1) (int_of_string_opt "4")
Sign up to request clarification or add additional context in comments.

1 Comment

For the paren-phobic: let n = Option.value ~default: ~-1 @@ int_of_string_opt "4"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.