Skip to main content
Version: 4.x (alpha)

Error handling

H3 does it's best to be robust to system failures or unexpected inputs, but some times these cannot be recovered from. H3's way of doing this is to return an error code to the caller.

Example#

This code example checks for an error when calling an H3 function and prints a message if the call did not succeed:

H3Error err;
H3Index result;
err = latLngToCell(lat, lng, res, &result);
if (err) {
fprintf(stderr, "Error: %d", err);
}

H3Error type#

The type returned by most H3 functions is H3Error, a 32 bit integer type with the following properties:

  • H3Error will be an integer type of 32 bits, i.e. uint32_t.
  • H3Error with value 0 indicates success (no error).
  • No H3Error value will set the most significant bit.
  • As a result of these properties, no H3Error value will set the bits that correspond with the Mode bit field in an H3Index.

32 bit return codes with the high bit never set allows for mixing error codes and resulting indexes if desired by the application, after copying the error codes into the result buffer.

Table of error codes#

ValueNameDescription
0E_SUCCESSSuccess (no error)
1E_FAILEDThe operation failed but a more specific error is not available
2E_DOMAINArgument was outside of acceptable range (when a more specific error code is not available)
3E_LATLNG_DOMAINLatitude or longitude arguments were outside of acceptable range
4E_RES_DOMAINResolution argument was outside of acceptable range
5E_CELL_INVALIDH3Index cell argument was not valid
6E_DIR_EDGE_INVALIDH3Index directed edge argument was not valid
7E_UNDIR_EDGE_INVALIDH3Index undirected edge argument was not valid
8E_VERTEX_INVALIDH3Index vertex argument was not valid
9E_PENTAGONPentagon distortion was encountered which the algorithm could not handle it
10E_DUPLICATE_INPUTDuplicate input was encountered in the arguments and the algorithm could not handle it
11E_NOT_NEIGHBORSH3Index cell arguments were not neighbors
12E_RES_MISMATCHH3Index cell arguments had incompatible resolutions
13E_MEMORYNecessary memory allocation failed
14E_MEMORY_BOUNDSBounds of provided memory were not large enough

The H3 library may always add additional error messages. Error messages not recognized by the application should be treated as E_FAILED.

Bindings#

Bindings translate error codes into the error handling mechanism appropriate to their language. For example, Java will convert error codes into Java Exceptions.

When possible, it is preferrable to retain the error code. When this is not possible it is fine to elide them. Language bindings should include error messages that are formatted as is usual in their language.

References#