diff --git a/book/src/binding/result.md b/book/src/binding/result.md index 733212a63..856d49ae7 100644 --- a/book/src/binding/result.md +++ b/book/src/binding/result.md @@ -137,10 +137,16 @@ you'd like for the Rust error to have. ...namespace behavior { ... template -static void trycatch(Try &&func, Fail &&fail) noexcept try { +static void trycatch(Try &&func, Fail &&fail) noexcept { +#if defined(RUST_CXX_NO_EXCEPTIONS) func(); -} catch (const std::exception &e) { - fail(e.what()); +#else + try { + func(); + } catch (const std::exception &e) { + fail(e.what()); + } +#endif } ... ...} // namespace behavior diff --git a/gen/src/builtin.rs b/gen/src/builtin.rs index d38473afc..37c33709b 100644 --- a/gen/src/builtin.rs +++ b/gen/src/builtin.rs @@ -410,10 +410,16 @@ pub(super) fn write(out: &mut OutFile) { " ::std::is_same(), ::std::declval())),", ); writeln!(out, " missing>::value>::type"); - writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept try {{"); + writeln!(out, "trycatch(Try &&func, Fail &&fail) noexcept {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); writeln!(out, " func();"); - writeln!(out, "}} catch (::std::exception const &e) {{"); - writeln!(out, " fail(e.what());"); + writeln!(out, "#else"); + writeln!(out, " try {{"); + writeln!(out, " func();"); + writeln!(out, " }} catch (::std::exception const &e) {{"); + writeln!(out, " fail(e.what());"); + writeln!(out, " }}"); + writeln!(out, "#endif"); writeln!(out, "}}"); out.end_block(Block::Namespace("behavior")); } diff --git a/gen/src/write.rs b/gen/src/write.rs index 77e1da0b2..7d3d5380d 100644 --- a/gen/src/write.rs +++ b/gen/src/write.rs @@ -1123,7 +1123,19 @@ fn write_rust_function_shim_impl( if sig.throws { out.builtin.rust_error = true; writeln!(out, " if (error$.ptr) {{"); + writeln!(out, "#if defined(RUST_CXX_NO_EXCEPTIONS)"); + writeln!( + out, + " const auto msg = static_cast(error$.ptr);" + ); + writeln!( + out, + " std::fprintf(stderr, \"Error: %s Aborting.\\n\", msg);" + ); + writeln!(out, " std::abort();"); + writeln!(out, "#else"); writeln!(out, " throw ::rust::impl<::rust::Error>::error(error$);"); + writeln!(out, "#endif"); writeln!(out, " }}"); } if indirect_return {