NodeC  0.1
Exceptions and Finally clauses

Functions for standard exceptions and finally handlers. More...

Data Structures

struct  lh_exception
 Standard exception type. More...
 

Macros

#define defer_exit(after, release_fun, local)
 Install a finally function that is always run. More...
 
#define defer(release_fun, local)
 Install a finally function that is always run. More...
 
#define on_abort(release_fun, local)
 

Functions

void lh_exception_free (lh_exception *exn)
 Free an exception.
 
lh_exceptionlh_exception_alloc_ex (int code, const char *msg, void *data, int _is_alloced)
 Create an exception.
 
lh_exceptionlh_exception_alloc_strdup (int code, const char *msg)
 Create an exception.
 
lh_exceptionlh_exception_alloc (int code, const char *msg)
 Create an exception.
 
void lh_throw (const lh_exception *e)
 Throw an exception.
 
void lh_throw_errno (int eno)
 
void lh_throw_str (int code, const char *msg)
 
void lh_throw_strdup (int code, const char *msg)
 
void lh_throw_cancel ()
 
lh_exceptionlh_exception_alloc_cancel ()
 
bool lh_exception_is_cancel (const lh_exception *exn)
 
void lh_strerror (char *buf, size_t len, int eno)
 Portable way to get a string error message.
 
lh_value lh_try (lh_exception **exn, lh_actionfun *action, lh_value arg)
 Convert an exceptional computation to an exceptional value. More...
 
lh_value lh_try_all (lh_exception **exn, lh_actionfun *action, lh_value arg)
 
lh_value lh_finally (lh_actionfun *action, lh_value arg, lh_releasefun *faction, lh_value farg)
 

Detailed Description

Functions for standard exceptions and finally handlers.


Data Structure Documentation

◆ lh_exception

struct lh_exception

Standard exception type.

Don't use this directly but use the provided functions to allocate and operate on exceptions.

Data Fields
int _is_alloced 0: static, bits: 0:exception, 1:msg, 2:data, determines if needs free
int code Exception code, usually an errno_t.
void * data Optional user data.
const char * msg Optional message.

Macro Definition Documentation

◆ defer

#define defer (   release_fun,
  local 
)

Install a finally function that is always run.

Parameters
release_funA lh_releasefun that is called on the exit of the scope, either when returning normally, or when aborting, for example when an exception is thrown.
localAn lh_value that is passed to the release function.

defer always needs a scope with double braces. For example:

void* p = nodec_alloc(10);
{defer(nodec_freev,lh_value_ptr(p)){
...
}}

◆ defer_exit

#define defer_exit (   after,
  release_fun,
  local 
)

Install a finally function that is always run.

Parameters
release_funA lh_releasefun that is called on the exit of the scope, either when returning normally, or when aborting, for example when an exception is thrown.
localAn lh_value that is passed to the release function.

defer always needs a scope with double braces. For example:

void* p = nodec_alloc(10);
{defer(nodec_freev,lh_value_ptr(p)){
...
}}

Function Documentation

◆ lh_try()

lh_value lh_try ( lh_exception **  exn,
lh_actionfun action,
lh_value  arg 
)

Convert an exceptional computation to an exceptional value.

If an exception is thrown, exn will be set to a non-null value