LightlyEdge C++ SDK
|
If your application can use exceptions, LightlyEdge has functions that throw an instance of lightly_edge_sdk::Exception
if they encounter an error, and return the value otherwise.
Remember to handle the exceptions at an appropriate place in your code.
If your application can't use exceptions, LightlyEdge also offers noexcept
functions, i.e. functions that do not throw exceptions. Every such function returns either a value, or an error wrapped in the tl::expected
class. They are recognizable by a name that ends in _noexcept
and also by the keyword noexcept
in their signature.
This kind of error handling is the standard way of error handling in some languages, e.g. Rust.
When a function in LightlyEdge C++ SDK wants to return a value of type T
, it instead returns the type:
This type can hold either the type T
, or lightly_edge_sdk::Error
, but not both. This is conceptually similar to C++ union
. From C++20 it is part of the standard library as std::expected
. To support C++11 and higher, LightlyEdge uses the backport tl::expected
with identical functionality.
The tl::expected
class template provides a collection of functions to access the value or error fields, which we showcase on LightlyEdge instantiation.
The function lightly_edge_sdk::LightlyEdge::new_from_tar_noexcept
returns tl::expected<lightly_edge_sdk::LightlyEdge, lightly_edge_sdk::Error>
. We first check if result.has_value()
. If not, we access result.error()
and print its message. If yes, we instead access the value by result.value()
. For convenience, we assign the created LightlyEdge to a new variable.