Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Last active May 17, 2018 11:06
Show Gist options
  • Select an option

  • Save jtpaasch/2ace574cc63901d4747c775e18d3d2da to your computer and use it in GitHub Desktop.

Select an option

Save jtpaasch/2ace574cc63901d4747c775e18d3d2da to your computer and use it in GitHub Desktop.
Handle custom errors (OCaml)
exception Foo of string
let main () =
print_endline "Running";
raise (Foo "Something went wrong")
let handle_errors f () =
Printexc.register_printer
(function
| Foo msg ->
Some (Printf.sprintf "Something foo: %s" msg)
| _ -> None
);
try
Unix.handle_unix_error main ()
with e ->
let msg = Printexc.to_string e in
Printf.printf "Error. %s\n%!" msg;
exit 1
let () = handle_errors main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment