Module Base__Import.Fun
Function manipulation.
- since
- 4.08
Combinators
val const : 'a -> _ -> 'a
const c
is a function that always returns the valuec
. For any argumentx
,(const c) x
isc
.
Exception handling
val protect : finally:(unit -> unit) -> (unit -> 'a) -> 'a
protect ~finally work
invokeswork ()
and thenfinally ()
beforework ()
returns with its value or an exception. In the latter case the exception is re-raised afterfinally ()
. Iffinally ()
raises an exception, then the exceptionFinally_raised
is raised instead.protect
can be used to enforce local invariants whetherwork ()
returns normally or raises an exception. However, it does not protect against unexpected exceptions raised insidefinally ()
such asStdlib
.Out_of_memory,Stdlib
.Stack_overflow, or asynchronous exceptions raised by signal handlers (e.g.Sys
.Break).Note: It is a programming error if other kinds of exceptions are raised by
finally
, as any exception raised inwork ()
will be lost in the event of aFinally_raised
exception. Therefore, one should make sure to handle those inside the finally.
exception
Finally_raised of exn
Finally_raised exn
is raised byprotect ~finally work
whenfinally
raises an exceptionexn
. This exception denotes either an unexpected exception or a programming error. As a general rule, one should not catch aFinally_raised
exception except as part of a catch-all handler.