fmt icon indicating copy to clipboard operation
fmt copied to clipboard

Support std::exception

Open fekir opened this issue 2 years ago • 1 comments

Exceptions are not supported, as they are not formattable:

int main() try {
  std::vector <int> vec;
  vec.at(42);
} catch(const std::out_of_range& ex){
    fmt::print("{}", ex);
}

https://godbolt.org/z/ejTMEYr3b

In practice, the expected/desired behavior would be to print the error message. Ie if they would behave as-if

int main() try {
  std::vector<int> vec;
  vec.at(42);
} catch(const std::out_of_range& ex){
  fmt::print("{}", ex.what());
}

This can be accomplished by defining a custom formatter for std::exception, but as fmt already provides formatters for many/most types from the standard library, support for std::exception should be part of it.

fekir avatar Jul 07 '22 12:07 fekir

Sure, a PR to add a formatter specialization is welcome.

vitaut avatar Jul 11 '22 23:07 vitaut