Catching Errors
The following example illustrates how to catch TileDB errors.
C
C++
Python
R
Java
Go
1
// Create TileDB context
2
tiledb_ctx_t* ctx;
3
tiledb_ctx_alloc(NULL, &ctx);
4
5
// Create a group. The code below creates a group `my_group` and prints a
6
// message because (normally) it will succeed.
7
tiledb_group_create(ctx, "my_group");
8
9
// Create the same group again. If we attempt to create the same group
10
// `my_group` as shown below, TileDB will return an error.
11
int rc = tiledb_group_create(ctx, "my_group");
12
if (rc == TILEDB_OK) {
13
// Retrieve the last error that occurred
14
tiledb_error_t* err = NULL;
15
tiledb_ctx_get_last_error(ctx, &err);
16
17
// Retrieve and print the error message.
18
const char* msg;
19
tiledb_error_message(err, &msg);
20
printf("%s\n", msg);
21
22
// Clean up error
23
tiledb_error_free(&err);
24
}
25
26
// Clean up
27
tiledb_ctx_free(&ctx);
Copied!
1
// Create TileDB context
2
Context ctx;
3
4
// Catch an error
5
try {
6
// Create a group. The code below creates a group `my_group` and prints a
7
// message because (normally) it will succeed.
8
create_group(ctx, "my_group");
9
10
// Create the same group again. If we attempt to create the same group
11
// `my_group` as shown below, TileDB will return an error.
12
create_group(ctx, "my_group");
13
} catch (tiledb::TileDBError& e) {
14
std::cout << "TileDB exception:\n" << e.what() << "\n";
15
}
16
17
// Set a different error handler
18
ctx.set_error_handler([](std::string msg) {
19
std::cout << "Callback:\n" << msg << "\n";
20
});
21
22
// The following will print the error message using the error
23
// handler set above
24
create_group(ctx, "my_group");
Copied!
1
# The Python API raises two kinds of errors:
2
# - Python errors, such as ValueError and KeyError
3
# - tiledb.libtiledb.TileDBError from libtiledb
4
import tiledb
5
from tiledb.libtiledb import TileDBError
6
7
try:
8
tiledb.group_create("my_group")
9
10
# Create the same group again
11
tiledb.group_create("my_group")
12
except TileDBError as err:
13
print("TileDB exception: ", err)
Copied!
1
# Create TileDB context
2
ctx <- tiledb_ctx()
3
4
result = tryCatch({
5
# Create a group. The code below creates a group `my_group` and prints a
6
# message because (normally) it will succeed.
7
tiledb_group_create("my_group", ctx=ctx)
8
9
# Create the same group again. If we attempt to create the same group
10
# `my_group` as shown below, TileDB will return an error.
11
tiledb_group_create("my_group", ctx=ctx)
12
}, warning = function(w) {
13
cat(w)
14
}, error = function(e) {
15
cat(e)
16
}, finally = {
17
}
Copied!
1
try {
2
ctx = new Context();
3
// Create a group. The code below creates a group `my_group` and prints a
4
// message because (normally) it will succeed.
5
Group group1 = new Group(ctx, "my_group");
6
7
// Create the same group again. If we attempt to create the same group
8
// `my_group` as shown below, TileDB will return an error.
9
Group group2 = new Group(ctx, "my_group");
10
} catch (TileDBError tileDBError) {
11
tileDBError.printStackTrace();
12
}
Copied!
1
// Create TileDB context
2
ctx, _ := NewContext(nil)
3
4
// Create a group. The code below creates a group `my_group` and prints a
5
// message because (normally) it will succeed.
6
tiledb.GroupCreate(ctx, "my_group");
7
8
// Create the same group again. If we attempt to create the same group
9
// `my_group` as shown below, TileDB will return an error.
10
err := tiledb.GroupCreate(ctx, "my_group");
11
if err != nil {
12
// Retrieve the last error that occurred
13
// Clean up error
14
err2 = ctx.LastError()
15
16
// Retrieve and print the error message.
17
fmt.Println(err2)
18
}
19
20
// Clean up
21
ctx.Free()
Copied!
Last modified 1yr ago
Copy link