A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://cloud.google.com/cpp/docs/reference/storage/2.36.0/storage-error below:

C++ Client Libraries | Google Cloud

Skip to main content

Stay organized with collections Save and categorize content based on your preferences.

Error Handling

This library never throws exceptions to signal errors. In general, the library returns a StatusOr. 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 to determine if there was an error. In these cases no StatusOr wrapper is used.

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.

Error Handling Example (without exceptions)

Applications that do not use exceptions to signal errors should check if the StatusOr<T> contains a value before using it. If the StatusOr<T> does contain a value then the StatusOr<T> can be used as a smart pointer to T. That is, operator->() and operator*() work as you would expect. If the StatusOr<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 the StatusOr<T> object. This will return a T if the StatusOr<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";
}

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-08-14 UTC.

[[["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-08-14 UTC."],[[["This page provides documentation for various versions of a C++ storage library, ranging from version 2.11.0 up to the release candidate 2.37.0-rc, with version 2.36.0 highlighted."],["The library handles errors by returning a `StatusOr\u003cT\u003e` object rather than throwing exceptions, allowing applications to check for the presence of a value before using it."],["`StatusOr\u003cT\u003e` objects can be treated as smart pointers to the contained value `T`, allowing for access using `operator-\u003e()` and `operator*()` when a value is present."],["Applications can use the `.value()` member function to throw a `RuntimeStatusError` if the `StatusOr\u003cT\u003e` does not contain a value, for cases when exceptions are used."],["The library also uses existing error mechanisms of some objects, like the state flags of `std::istream` in the case of `ReadObject()`, instead of utilizing the `StatusOr\u003cT\u003e` wrapper."]]],[]]


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4