This library never throws exceptions to signal errors. In general, the library returns aStatusOr. Some functions return objects that already have an existing error handling mechanism. For example,ReadObject()returns a type derived fromstd::istreamwhere the application can check thestate flagsto determine if there was an error. In these cases noStatusOrwrapper is used.
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.
Error Handling Example (without exceptions)
Applications that do not use exceptions to signal errors should check if theStatusOr<T>contains a value before using it. If theStatusOr<T>does contain a value then theStatusOr<T>can be used as a smart pointer toT. That is,operator->()andoperator*()work as you would expect. If theStatusOr<T>does not contain a value then the error details are available using the.status()member function (and trying to access the value produces undefined behavior).
using gcs = ::google::cloud::storage;
[](gcs::Client client) {
google::cloud::StatusOr<gcs::BucketMetadata> metadata =
client.GetBucketMetadata("my-bucket");
if (!metadata) {
std::cerr << "GetBucketMetadata: " << metadata.status() << "\n";
return;
}
// use `metadata` as a smart pointer to `BucketMetadata`
std::cout << "The metadata for bucket " << metadata->name()
<< " is " << *metadata << "\n";
}
Error Handling Example (with exceptions)
Applications that use exceptions to signal errors can simply call.value()on theStatusOr<T>object. This will return aTif theStatusOr<T>object contains a value, and will otherwise throw an exception.
using gcs = ::google::cloud::storage;
[](gcs::Client client) {
gcs::BucketMetadata metadata = client.GetBucketMetadata(
"my-bucket").value(); // throws on error
std::cout << "The metadata for bucket " << metadata.name()
<< " is " << metadata << "\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."],[],[],null,["Version 2.38.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/storage/latest/storage-error)\n- [2.41.0](/cpp/docs/reference/storage/2.41.0/storage-error)\n- [2.40.0](/cpp/docs/reference/storage/2.40.0/storage-error)\n- [2.39.0](/cpp/docs/reference/storage/2.39.0/storage-error)\n- [2.38.0](/cpp/docs/reference/storage/2.38.0/storage-error)\n- [2.37.0](/cpp/docs/reference/storage/2.37.0/storage-error)\n- [2.36.0](/cpp/docs/reference/storage/2.36.0/storage-error)\n- [2.35.0](/cpp/docs/reference/storage/2.35.0/storage-error)\n- [2.34.0](/cpp/docs/reference/storage/2.34.0/storage-error)\n- [2.33.0](/cpp/docs/reference/storage/2.33.0/storage-error)\n- [2.32.0](/cpp/docs/reference/storage/2.32.0/storage-error)\n- [2.31.0](/cpp/docs/reference/storage/2.31.0/storage-error)\n- [2.30.0](/cpp/docs/reference/storage/2.30.0/storage-error)\n- [2.29.0](/cpp/docs/reference/storage/2.29.0/storage-error)\n- [2.28.0](/cpp/docs/reference/storage/2.28.0/storage-error)\n- [2.27.0](/cpp/docs/reference/storage/2.27.0/storage-error)\n- [2.26.0](/cpp/docs/reference/storage/2.26.0/storage-error)\n- [2.25.1](/cpp/docs/reference/storage/2.25.1/storage-error)\n- [2.24.0](/cpp/docs/reference/storage/2.24.0/storage-error)\n- [2.23.0](/cpp/docs/reference/storage/2.23.0/storage-error)\n- [2.22.1](/cpp/docs/reference/storage/2.22.1/storage-error)\n- [2.21.0](/cpp/docs/reference/storage/2.21.0/storage-error)\n- [2.20.0](/cpp/docs/reference/storage/2.20.0/storage-error)\n- [2.19.0](/cpp/docs/reference/storage/2.19.0/storage-error)\n- [2.18.0](/cpp/docs/reference/storage/2.18.0/storage-error)\n- [2.17.0](/cpp/docs/reference/storage/2.17.0/storage-error)\n- [2.16.0](/cpp/docs/reference/storage/2.16.0/storage-error)\n- [2.15.1](/cpp/docs/reference/storage/2.15.1/storage-error)\n- [2.14.0](/cpp/docs/reference/storage/2.14.0/storage-error)\n- [2.13.0](/cpp/docs/reference/storage/2.13.0/storage-error)\n- [2.12.0](/cpp/docs/reference/storage/2.12.0/storage-error)\n- [2.11.0](/cpp/docs/reference/storage/2.11.0/storage-error) \n\nError Handling\n==============\n\nThis library never throws exceptions to signal errors. In general, the library returns a [StatusOr](https://cloud.google.com/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1StatusOr.html). Some functions return objects that already have an existing error handling mechanism. For example, `ReadObject()` returns a type derived from `std::istream` where the application can check the [state flags](https://en.cppreference.com/w/cpp/io/basic_ios/rdstate) to determine if there was an error. In these cases no `StatusOr` wrapper is used.\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\nError Handling Example (without exceptions)\n-------------------------------------------\n\nApplications that do not use exceptions to signal errors should check if the `StatusOr\u003cT\u003e` contains a value before using it. If the `StatusOr\u003cT\u003e` does contain a value then the `StatusOr\u003cT\u003e` can be used as a smart pointer to `T`. That is, `operator-\u003e()` and `operator*()` work as you would expect. If the `StatusOr\u003cT\u003e` does not contain a value then the error details are available using the `.status()` member function (and trying to access the value produces undefined behavior). \n\n using gcs = ::google::cloud::storage;\n [](gcs::Client client) {\n google::cloud::StatusOr\u003cgcs::BucketMetadata\u003e metadata =\n client.GetBucketMetadata(\"my-bucket\");\n\n if (!metadata) {\n std::cerr \u003c\u003c \"GetBucketMetadata: \" \u003c\u003c metadata.status() \u003c\u003c \"\\n\";\n return;\n }\n\n // use `metadata` as a smart pointer to `BucketMetadata`\n std::cout \u003c\u003c \"The metadata for bucket \" \u003c\u003c metadata-\u003ename()\n \u003c\u003c \" is \" \u003c\u003c *metadata \u003c\u003c \"\\n\";\n }\n\nError Handling Example (with exceptions)\n----------------------------------------\n\nApplications that use exceptions to signal errors can simply call `.value()` on the `StatusOr\u003cT\u003e` object. This will return a `T` if the `StatusOr\u003cT\u003e` object contains a value, and will otherwise throw an exception. \n\n using gcs = ::google::cloud::storage;\n [](gcs::Client client) {\n gcs::BucketMetadata metadata = client.GetBucketMetadata(\n \"my-bucket\").value(); // throws on error\n std::cout \u003c\u003c \"The metadata for bucket \" \u003c\u003c metadata.name()\n \u003c\u003c \" is \" \u003c\u003c metadata \u003c\u003c \"\\n\";\n }"]]