In general, the library returns aStatusOrif an error is possible. Some functions return objects that are not wrapped in aStatusOr<>but will themselves return aStatusOr<>to signal an error. For example, wrappers for asynchronous operations returnfuture<StatusOr<T>>.
Applications should check if theStatusOr<T>contains a value before using it, much like how you might check that a pointer is not null before dereferencing it. Indeed, aStatusOr<T>object can be used like a smart-pointer toT, with the main difference being that when it does not hold aTit will instead hold aStatusobject with extra information about the error.
You can check that aStatusOr<T>contains a value by calling the.ok()method, or by usingoperator bool()(like with other smart pointers). If there is no value, you can access the containedStatusobject using the.status()member. If there is a value, you may access it by dereferencing withoperator*()oroperator->(). As with all smart pointers, callers must first check that theStatusOr<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 aRuntimeStatusErrorif there is no value.
Example
namespace pubsub = ::google::cloud::pubsub;
[](pubsub::TopicAdminClient client, std::string const& project_id) {
// The actual type of `topic` is
// google::cloud::StatusOr<google::pubsub::v1::Topic>, but
// we expect it'll most often be declared with auto like this.
for (auto& topic : client.ListTopics(project_id)) {
// Use `topic` like a smart pointer; check it before de-referencing
if (!topic) {
// `topic` doesn't contain a value, so `.status()` will contain error
// info
std::cerr << topic.status() << "\n";
break;
}
std::cout << topic->DebugString() << "\n";
}
}
See Also
google::cloud::futurefor more details on the type returned by asynchronous operations.
[[["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 documentation covers error handling for the C++ Pub/Sub library, with the latest release candidate being version 2.37.0-rc.\u003c/p\u003e\n"],["\u003cp\u003eThe library uses \u003ccode\u003eStatusOr<T>\u003c/code\u003e to manage errors, which either holds a value of type \u003ccode\u003eT\u003c/code\u003e or a \u003ccode\u003eStatus\u003c/code\u003e object detailing the error.\u003c/p\u003e\n"],["\u003cp\u003eApplications need to verify if a \u003ccode\u003eStatusOr<T>\u003c/code\u003e contains a value using methods like \u003ccode\u003e.ok()\u003c/code\u003e or \u003ccode\u003eoperator bool()\u003c/code\u003e before accessing it to prevent errors.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003e.value()\u003c/code\u003e member function can be used to access the contained value, but it will throw a \u003ccode\u003eRuntimeStatusError\u003c/code\u003e if no value is present, or terminate the program if exceptions are disabled.\u003c/p\u003e\n"],["\u003cp\u003eAsynchronous operations return a \u003ccode\u003efuture<StatusOr<T>>\u003c/code\u003e type.\u003c/p\u003e\n"]]],[],null,["Version 2.19.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/pubsub/latest/pubsub-error-handling)\n- [2.41.0](/cpp/docs/reference/pubsub/2.41.0/pubsub-error-handling)\n- [2.40.0](/cpp/docs/reference/pubsub/2.40.0/pubsub-error-handling)\n- [2.39.0](/cpp/docs/reference/pubsub/2.39.0/pubsub-error-handling)\n- [2.38.0](/cpp/docs/reference/pubsub/2.38.0/pubsub-error-handling)\n- [2.37.0](/cpp/docs/reference/pubsub/2.37.0/pubsub-error-handling)\n- [2.36.0](/cpp/docs/reference/pubsub/2.36.0/pubsub-error-handling)\n- [2.35.0](/cpp/docs/reference/pubsub/2.35.0/pubsub-error-handling)\n- [2.34.0](/cpp/docs/reference/pubsub/2.34.0/pubsub-error-handling)\n- [2.33.0](/cpp/docs/reference/pubsub/2.33.0/pubsub-error-handling)\n- [2.32.0](/cpp/docs/reference/pubsub/2.32.0/pubsub-error-handling)\n- [2.31.0](/cpp/docs/reference/pubsub/2.31.0/pubsub-error-handling)\n- [2.30.0](/cpp/docs/reference/pubsub/2.30.0/pubsub-error-handling)\n- [2.29.0](/cpp/docs/reference/pubsub/2.29.0/pubsub-error-handling)\n- [2.28.0](/cpp/docs/reference/pubsub/2.28.0/pubsub-error-handling)\n- [2.27.0](/cpp/docs/reference/pubsub/2.27.0/pubsub-error-handling)\n- [2.26.0](/cpp/docs/reference/pubsub/2.26.0/pubsub-error-handling)\n- [2.25.1](/cpp/docs/reference/pubsub/2.25.1/pubsub-error-handling)\n- [2.24.0](/cpp/docs/reference/pubsub/2.24.0/pubsub-error-handling)\n- [2.23.0](/cpp/docs/reference/pubsub/2.23.0/pubsub-error-handling)\n- [2.22.1](/cpp/docs/reference/pubsub/2.22.1/pubsub-error-handling)\n- [2.21.0](/cpp/docs/reference/pubsub/2.21.0/pubsub-error-handling)\n- [2.20.0](/cpp/docs/reference/pubsub/2.20.0/pubsub-error-handling)\n- [2.19.0](/cpp/docs/reference/pubsub/2.19.0/pubsub-error-handling)\n- [2.18.0](/cpp/docs/reference/pubsub/2.18.0/pubsub-error-handling)\n- [2.17.0](/cpp/docs/reference/pubsub/2.17.0/pubsub-error-handling)\n- [2.16.0](/cpp/docs/reference/pubsub/2.16.0/pubsub-error-handling)\n- [2.15.1](/cpp/docs/reference/pubsub/2.15.1/pubsub-error-handling)\n- [2.14.0](/cpp/docs/reference/pubsub/2.14.0/pubsub-error-handling)\n- [2.13.0](/cpp/docs/reference/pubsub/2.13.0/pubsub-error-handling)\n- [2.12.0](/cpp/docs/reference/pubsub/2.12.0/pubsub-error-handling)\n- [2.11.0](/cpp/docs/reference/pubsub/2.11.0/pubsub-error-handling) \n\nError Handling\n==============\n\nIn 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\u003c\u003e` but will themselves return a `StatusOr\u003c\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` 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 pubsub = ::google::cloud::pubsub;\n [](pubsub::TopicAdminClient client, std::string const& project_id) {\n // The actual type of `topic` is\n // google::cloud::StatusOr\u003cgoogle::pubsub::v1::Topic\u003e, but\n // we expect it'll most often be declared with auto like this.\n for (auto& topic : client.ListTopics(project_id)) {\n // Use `topic` like a smart pointer; check it before de-referencing\n if (!topic) {\n // `topic` doesn't contain a value, so `.status()` will contain error\n // info\n std::cerr \u003c\u003c topic.status() \u003c\u003c \"\\n\";\n break;\n }\n std::cout \u003c\u003c topic-\u003eDebugString() \u003c\u003c \"\\n\";\n }\n }\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."]]