This library never throws exceptions to signal error. In general, the library returns a StatusOr if an error is possible. Some functions return objects that are not wrapped in a StatusOr<T> but will themselves return a StatusOr<T> to signal an error. For example, wrappers for asynchronous operations return future<StatusOr<T>>.
Applications should check if the StatusOr<T> contains a value before using it, much like how you might check that a pointer is not null before dereferencing it. Indeed, a StatusOr<T> object can be used like a smart-pointer to T, with the main difference being that when it does not hold a T it will instead hold a Status object with extra information about the error.
You can check that a StatusOr<T> contains a value by calling the .ok() method, or by using operator bool() (like with other smart pointers). If there is no value, you can access the contained Status object using the .status() member. If there is a value, you may access it by dereferencing with operator*() or operator->(). As with all smart pointers, callers must first check that the StatusOr<T> contains a value before dereferencing and accessing the contained value. Alternatively, callers may instead use the .value() member function which is defined to throw a RuntimeStatusError if there is no value.
Example
namespace spanner = ::google::cloud::spanner;
[](spanner::Client client) {
auto rows = client.Read("Albums", spanner::KeySet::All(), {"AlbumTitle"});
// The actual type of `row` is google::cloud::StatusOr<spanner::Row>, but
// we expect it'll most often be declared with auto like this.
for (auto const& row : rows) {
// Use `row` like a smart pointer; check it before dereferencing
if (!row) {
// `row` doesn't contain a value, so `.status()` will contain error info
std::cerr << row.status();
break;
}
// The actual type of `song` is google::cloud::StatusOr<std::string>, but
// again we expect it'll be commonly declared with auto as we show here.
auto song = row->get<std::string>("AlbumTitle");
// Instead of checking then dereferencing `song` as we did with `row`
// above, here we demonstrate use of the `.value()` member, which will
// return a reference to the contained `T` if it exists, otherwise it
// will throw an exception (or terminate if compiled without exceptions).
std::cout << "SongName: " << song.value() << "\n";
}
}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe latest version of the documentation is 2.37.0-rc, and there are several older versions available, ranging from 2.36.0 down to 2.11.0, that cover spanner error handling.\u003c/p\u003e\n"],["\u003cp\u003eThe library uses \u003ccode\u003eStatusOr\u003c/code\u003e to indicate errors instead of throwing exceptions.\u003c/p\u003e\n"],["\u003cp\u003eApplications should verify that a \u003ccode\u003eStatusOr<T>\u003c/code\u003e contains a value using \u003ccode\u003e.ok()\u003c/code\u003e or \u003ccode\u003eoperator bool()\u003c/code\u003e before attempting to access the value.\u003c/p\u003e\n"],["\u003cp\u003eIf \u003ccode\u003eStatusOr<T>\u003c/code\u003e does not have a value, you can access the error information via the \u003ccode\u003e.status()\u003c/code\u003e member.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e.value()\u003c/code\u003e member function can be used to retrieve a \u003ccode\u003eStatusOr<T>\u003c/code\u003e value, but it will throw an exception or terminate the program if no value is present.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/spanner/latest/spanner-error-handling)\n- [2.41.0](/cpp/docs/reference/spanner/2.41.0/spanner-error-handling)\n- [2.40.0](/cpp/docs/reference/spanner/2.40.0/spanner-error-handling)\n- [2.39.0](/cpp/docs/reference/spanner/2.39.0/spanner-error-handling)\n- [2.38.0](/cpp/docs/reference/spanner/2.38.0/spanner-error-handling)\n- [2.37.0](/cpp/docs/reference/spanner/2.37.0/spanner-error-handling)\n- [2.36.0](/cpp/docs/reference/spanner/2.36.0/spanner-error-handling)\n- [2.35.0](/cpp/docs/reference/spanner/2.35.0/spanner-error-handling)\n- [2.34.0](/cpp/docs/reference/spanner/2.34.0/spanner-error-handling)\n- [2.33.0](/cpp/docs/reference/spanner/2.33.0/spanner-error-handling)\n- [2.32.0](/cpp/docs/reference/spanner/2.32.0/spanner-error-handling)\n- [2.31.0](/cpp/docs/reference/spanner/2.31.0/spanner-error-handling)\n- [2.30.0](/cpp/docs/reference/spanner/2.30.0/spanner-error-handling)\n- [2.29.0](/cpp/docs/reference/spanner/2.29.0/spanner-error-handling)\n- [2.28.0](/cpp/docs/reference/spanner/2.28.0/spanner-error-handling)\n- [2.27.0](/cpp/docs/reference/spanner/2.27.0/spanner-error-handling)\n- [2.26.0](/cpp/docs/reference/spanner/2.26.0/spanner-error-handling)\n- [2.25.1](/cpp/docs/reference/spanner/2.25.1/spanner-error-handling)\n- [2.24.0](/cpp/docs/reference/spanner/2.24.0/spanner-error-handling)\n- [2.23.0](/cpp/docs/reference/spanner/2.23.0/spanner-error-handling)\n- [2.22.1](/cpp/docs/reference/spanner/2.22.1/spanner-error-handling)\n- [2.21.0](/cpp/docs/reference/spanner/2.21.0/spanner-error-handling)\n- [2.20.0](/cpp/docs/reference/spanner/2.20.0/spanner-error-handling)\n- [2.19.0](/cpp/docs/reference/spanner/2.19.0/spanner-error-handling)\n- [2.18.0](/cpp/docs/reference/spanner/2.18.0/spanner-error-handling)\n- [2.17.0](/cpp/docs/reference/spanner/2.17.0/spanner-error-handling)\n- [2.16.0](/cpp/docs/reference/spanner/2.16.0/spanner-error-handling)\n- [2.15.1](/cpp/docs/reference/spanner/2.15.1/spanner-error-handling)\n- [2.14.0](/cpp/docs/reference/spanner/2.14.0/spanner-error-handling)\n- [2.13.0](/cpp/docs/reference/spanner/2.13.0/spanner-error-handling)\n- [2.12.0](/cpp/docs/reference/spanner/2.12.0/spanner-error-handling)\n- [2.11.0](/cpp/docs/reference/spanner/2.11.0/spanner-error-handling) \n\nError Handling\n==============\n\nThis library never throws exceptions to signal error. In general, the library returns a [StatusOr](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr.html) if an error is possible. Some functions return objects that are not wrapped in a `StatusOr\u003cT\u003e` but will themselves return a `StatusOr\u003cT\u003e` to signal an error. For example, wrappers for asynchronous operations return `future\u003cStatusOr\u003cT\u003e\u003e`.\n\nApplications should check if the `StatusOr\u003cT\u003e` contains a value before using it, much like how you might check that a pointer is not null before dereferencing it. Indeed, a `StatusOr\u003cT\u003e` object can be used like a smart-pointer to `T`, with the main difference being that when it does not hold a `T` it will instead hold a `Status` object with extra information about the error.\n\nYou can check that a `StatusOr\u003cT\u003e` contains a value by calling the `.ok()` method, or by using `operator bool()` (like with other smart pointers). If there is no value, you can access the contained `Status` object using the `.status()` member. If there is a value, you may access it by dereferencing with `operator*()` or `operator-\u003e()`. As with all smart pointers, callers must first check that the `StatusOr\u003cT\u003e` contains a value before dereferencing and accessing the contained value. Alternatively, callers may instead use the `.value()` member function which is defined to throw a [RuntimeStatusError](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1RuntimeStatusError.html) if there is no value.\n| **Note:** If you're compiling with exceptions disabled, calling `.value()` on a `StatusOr\u003cT\u003e` that does not contain a value will terminate the program instead of throwing.\n\n###### Example\n\n namespace spanner = ::google::cloud::spanner;\n [](spanner::Client client) {\n auto rows = client.Read(\"Albums\", spanner::KeySet::All(), {\"AlbumTitle\"});\n // The actual type of `row` is google::cloud::StatusOr\u003cspanner::Row\u003e, but\n // we expect it'll most often be declared with auto like this.\n for (auto const& row : rows) {\n // Use `row` like a smart pointer; check it before dereferencing\n if (!row) {\n // `row` doesn't contain a value, so `.status()` will contain error info\n std::cerr \u003c\u003c row.status();\n break;\n }\n\n // The actual type of `song` is google::cloud::StatusOr\u003cstd::string\u003e, but\n // again we expect it'll be commonly declared with auto as we show here.\n auto song = row-\u003eget\u003cstd::string\u003e(\"AlbumTitle\");\n\n // Instead of checking then dereferencing `song` as we did with `row`\n // above, here we demonstrate use of the `.value()` member, which will\n // return a reference to the contained `T` if it exists, otherwise it\n // will throw an exception (or terminate if compiled without exceptions).\n std::cout \u003c\u003c \"SongName: \" \u003c\u003c song.value() \u003c\u003c \"\\n\";\n }\n }\n\n###### See Also\n\n[`google::cloud::StatusOr`](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr.html)\n\n###### See Also\n\n[`google::cloud::Status`](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Status.html) the class used to describe errors.\n\n###### See Also\n\n[`google::cloud::future`](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1future.html) for more details on the type returned by asynchronous operations."]]