LightlyEdge C++ SDK
Loading...
Searching...
No Matches
Error Handling

Error Handling with Exceptions

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.

#include <iostream>
#include "lightly_edge_sdk.h"
using namespace lightly_edge_sdk;
int main() {
std::cout << "Initializing LightlyEdge..." << std::endl;
try {
auto lightly_edge = LightlyEdge::new_from_tar("../lightly_embedding_model_009.tar");
} catch (const Exception& ex) {
std::cout << ex.what() << std::endl;
return 1;
}
std::cout << "LightlyEdge successfully initialized!" << std::endl;
return 0;
}
Exception class for lightly_edge_sdk::value_or_throw function.
Definition lightly_edge_error.h:44
Namespace with core LightlyEdge SDK functionality.
Definition lightly_edge_error.h:15

Monadic Error Handling

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.

Returning Values with tl::expected

When a function in LightlyEdge C++ SDK wants to return a value of type T, it instead returns the type:

tl::expected<T, lightly_edge_sdk::Error>

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.

#include <iostream>
#include "lightly_edge_sdk.h"
using namespace lightly_edge_sdk;
int main() {
std::cout << "Initializing LightlyEdge..." << std::endl;
// Initialize LightlyEdge.
auto result = LightlyEdge::new_from_tar_noexcept("../lightly_embedding_model_009.tar");
// Check if the initialization was successful.
if (!result.has_value()) {
std::cout << result.error().message << std::endl;
return 1;
}
// Move the lightly_edge from the result to the lightly_edge variable for convenience.
// We cannot use a simple assignment because LightlyEdge can't be copied.
auto lightly_edge = std::move(result.value());
std::cout << "LightlyEdge successfully initialized!" << std::endl;
return 0;
}

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.