Last active
May 17, 2018 11:06
-
-
Save jtpaasch/2ace574cc63901d4747c775e18d3d2da to your computer and use it in GitHub Desktop.
Handle custom errors (OCaml)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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