Can we list all the run-time errors that are absent from OCaml programs?

Correct me if I am wrong (and let’s ignore the case of programs with C bindings, which are not pure OCaml programs):

  • null pointer exception
  • type cast exception
  • segmentation fault
  • memory leaks

I am trying to list all the run-time errors that you can’t get with an ocaml program, compared to languages such as Java and C++, for example.

1 Like

I am unsure about the type cast exception for example, since I never do object programming with OCaml.

The object system works like the rest of OCaml (I wonder why it’s considered as some sort of legendary beast that would not obey the usual rules): No runtime type information, only safe upcasts.

1 Like

Memory leaks are still possible because it is possible for a programmer to fail to “get rid of” a resource they don’t need any longer. Imagine, for example, that you are maintaining a data structure containing information you only need temporarily and you never removed stale data from the data structure.

What is not possible is to have use after free errors, or to leak memory that is no longer referenced/reachable by anything in the program.


If you compare to a dynamic language like Clojure, it would be difficult to catalog all of the errors that can’t happen in OCaml. (That’s not an insult–I’m a fan of Clojure, Common Lisp, etc.–but I recognize the tradeoffs.)

thanks for having corrected me about the memory leaks. Now my manuscript is a little bit more correct. :slight_smile:

If undefined behaviour in C counts, then I’d add unions, since it’s not exactly type cast exception I’d say.

You never get to access the “non-active” members in OCaml thanks to variant types.

It’s supposedly defined in C++ to access non-active members, but I know way too little about C++ to know what the definition in C++ actually means.

Accessing the “wrong” member of a Union in C is actually defined behavior, and undefined in C++. You have it backwards. The OCaml C runtime does just this to do bit pattern preserving transformations between types of the same size in a few places — it’s sanctioned to do this by the C standard.

It is, however, dangerous, and it’s indeed something that sum types in OCaml don’t permit.

1 Like