Andrew Feather
Andrew Feather

Reputation: 183

Not Found - Exception in OCaml

I'm trying to check a mapping I've made to see if there are any values that match the word I'm sending. But it should return zero when there are no instances found. Instead, it's throwing Exception: Not_found. and exiting.

Is there any way I can catch this error? I thought Some and None were supposed to do the trick.

let word_count word =
            match DictMap.find word word_mapping with
            | None -> 0
            | Some count -> count;;

Upvotes: 1

Views: 3309

Answers (1)

FPstudent
FPstudent

Reputation: 841

I assume that DictMap is some result of applying the Map functor. Use try-with (rather than the option type) since find raises an exception rather than returning None when the key is not found.

let word_count word = try DictMap.find word word_mapping with Not_found -> 0;;

Upvotes: 2

Related Questions