Tip of the Week #76: Use absl::Status
▻https://abseil.io/tips/76
Originally posted as TotW #76 on May 4, 2014
By Titus Winters
Updated 2020-02-06
Quicklink: abseil.io/tips/76
Some folks have questions about when and how to use absl::Status, so here are a few reasons why you ought to use Status, and some things to keep in mind when using it.
Communicate Intent and Force the Caller to Handle Errors
Use Status to force the caller to handle the possibility of errors. Since June 2013, a function returning an Status object cannot simply be ignored. That is, this code produces a compilation error:
absl::Status Foo();
void CallFoo1() Foo();
Whereas these calls are fine:
void CallFoo2() Foo().IgnoreError();
void CallFoo3() if (!status.ok()) std::abort();
void CallFoo4() absl::Status status = Foo(); if (!status.ok()) std::cerr << (...)