Search code examples
exceptioncompiler-errorspattern-matchingocamlutop

Getting error when try to pattern match for Unix_error (Ocaml)?


I've been trying to pattern for the case where a user types in an invalid directory:

# let dir = Unix.opendir "adfalf";;
Exception: Unix.Unix_error (Unix.ENOENT, "opendir", "adfalf"). 

My function is as follows:

let files_of_dir d = 
    try 
      let dir = Unix.opendir d in
      ...
    with
       Unix_error (uerr, ucommand, dir) -> raise Not_found

Except I keep getting the compilation error:

Error: This variant pattern is expected to have type exn
       The constructor Unix_error does not belong to type exn

I don't understand what I'm doing wrong w/ the pattern matching. If anyone could help me on this it would be greatly appreciated!


Some Other Notes:

I've been compiling my code using the following command on terminal:

ocamlbuild filename.byte

Solution

  • You need to say Unix.Unix_error, not just Unix_error. Note that this is what appears in your sample session.