From c07e2aa4b130b4fd359a1c6455a5201b17af01fa Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Mon, 7 Sep 2026 16:13:51 +0200 Subject: [PATCH] Disable std::exception_ptr and nested_exception formatters when exceptions are disabled. (#4904) Fixes building with Clang 20 and older with -fno-exceptions, which treats try/catch usage as an error. --- include/fmt/std.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/fmt/std.h b/include/fmt/std.h index 28a7f6c9..ed169de5 100644 --- a/include/fmt/std.h +++ b/include/fmt/std.h @@ -665,7 +665,7 @@ struct formatter< } #endif // FMT_USE_RTTI out = detail::write_bytes(out, string_view(ex.what())); -#if FMT_USE_RTTI +#if FMT_USE_RTTI && FMT_USE_EXCEPTIONS // If the exception carries a nested exception (e.g. via // std::throw_with_nested), format the whole chain. if (auto* nested = dynamic_cast(&ex)) { @@ -680,7 +680,7 @@ struct formatter< } } } -#endif // FMT_USE_RTTI +#endif // FMT_USE_RTTI && FMT_USE_EXCEPTIONS return out; } }; @@ -690,6 +690,7 @@ template <> struct formatter : formatter { auto format(const std::exception_ptr& ep, FormatContext& ctx) const -> decltype(ctx.out()) { if (!ep) return this->write_padded(ctx, string_view("none")); +#if FMT_USE_EXCEPTIONS try { std::rethrow_exception(ep); } catch (const std::exception& e) { @@ -697,6 +698,9 @@ template <> struct formatter : formatter { } catch (...) { return this->write_padded(ctx, string_view("unknown exception")); } +#else + return this->write_padded(ctx, string_view("unknown exception")); +#endif // FMT_USE_EXCEPTIONS } };