Catching Errors
The following example illustrates how to catch TileDB errors.
// Create TileDB context
tiledb_ctx_t* ctx;
tiledb_ctx_alloc(NULL, &ctx);
// Create a group. The code below creates a group `my_group` and prints a
// message because (normally) it will succeed.
tiledb_group_create(ctx, "my_group");
// Create the same group again. If we attempt to create the same group
// `my_group` as shown below, TileDB will return an error.
int rc = tiledb_group_create(ctx, "my_group");
if (rc == TILEDB_OK) {
// Retrieve the last error that occurred
tiledb_error_t* err = NULL;
tiledb_ctx_get_last_error(ctx, &err);
// Retrieve and print the error message.
const char* msg;
tiledb_error_message(err, &msg);
printf("%s\n", msg);
// Clean up error
tiledb_error_free(&err);
}
// Clean up
tiledb_ctx_free(&ctx);
// Create TileDB context
Context ctx;
// Catch an error
try {
// Create a group. The code below creates a group `my_group` and prints a
// message because (normally) it will succeed.
create_group(ctx, "my_group");
// Create the same group again. If we attempt to create the same group
// `my_group` as shown below, TileDB will return an error.
create_group(ctx, "my_group");
} catch (tiledb::TileDBError& e) {
std::cout << "TileDB exception:\n" << e.what() << "\n";
}
// Set a different error handler
ctx.set_error_handler([](std::string msg) {
std::cout << "Callback:\n" << msg << "\n";
});
// The following will print the error message using the error
// handler set above
create_group(ctx, "my_group");
# The Python API raises two kinds of errors:
# - Python errors, such as ValueError and KeyError
# - tiledb.libtiledb.TileDBError from libtiledb
import tiledb
from tiledb.libtiledb import TileDBError
try:
tiledb.group_create("my_group")
# Create the same group again
tiledb.group_create("my_group")
except TileDBError as err:
print("TileDB exception: ", err)
# Create TileDB context
ctx <- tiledb_ctx()
result = tryCatch({
# Create a group. The code below creates a group `my_group` and prints a
# message because (normally) it will succeed.
tiledb_group_create("my_group", ctx=ctx)
# Create the same group again. If we attempt to create the same group
# `my_group` as shown below, TileDB will return an error.
tiledb_group_create("my_group", ctx=ctx)
}, warning = function(w) {
cat(w)
}, error = function(e) {
cat(e)
}, finally = {
}
try {
ctx = new Context();
// Create a group. The code below creates a group `my_group` and prints a
// message because (normally) it will succeed.
Group.create(ctx, "test_group");
// Create the same group again. If we attempt to create the same group
// `my_group` as shown below, TileDB will return an error.
Group.create(ctx, "test_group");
} catch (TileDBError tileDBError) {
tileDBError.printStackTrace();
}
// Create TileDB context
ctx, _ := NewContext(nil)
// Create a group. The code below creates a group `my_group` and prints a
// message because (normally) it will succeed.
tiledb.GroupCreate(ctx, "my_group");
// Create the same group again. If we attempt to create the same group
// `my_group` as shown below, TileDB will return an error.
err := tiledb.GroupCreate(ctx, "my_group");
if err != nil {
// Retrieve the last error that occurred
// Clean up error
err2 = ctx.LastError()
// Retrieve and print the error message.
fmt.Println(err2)
}
// Clean up
ctx.Free()
Last updated