@@ -523,19 +523,34 @@ namespace stdexec {
523523# include < cuda_runtime_api.h>
524524#endif
525525
526+ // The following macros are used to conditionally compile exception handling code. They
527+ // are used in the same way as `try` and `catch` blocks, but they allow for different
528+ // behavior based on whether exceptions are enabled or not, and whether the code is being
529+ // compiled for device or not.
530+ //
531+ // Usage:
532+ // STDEXEC_TRY({
533+ // ... // Code that may throw an exception
534+ // })
535+ // STDEXEC_CATCH_OR((cuda_error& e) { // Handle CUDA exceptions
536+ // ...
537+ // })
538+ // STDEXEC_CATCH((...) { // Handle any other exceptions
539+ // ...
540+ // })
526541// clang-format off
527542#if STDEXEC_NO_EXCEPTIONS() || (STDEXEC_CUDA_COMPILATION() && defined(__CUDA_ARCH__))
528- # define STDEXEC_TRY
529- # define STDEXEC_CATCH (...) STDEXEC_CATCH_I
530- # define STDEXEC_CATCH_I (...)
543+ # define STDEXEC_TRY (...) { __VA_ARGS__ }
544+ # define STDEXEC_CATCH (...)
545+ # define STDEXEC_CATCH_OR (...)
531546#elif STDEXEC_CUDA_COMPILATION() && STDEXEC_NVHPC()
532- # define STDEXEC_TRY if target (nv::target::is_host ) { try
533- # define STDEXEC_CATCH (...) catch ( __VA_ARGS__) STDEXEC_CATCH_I
534- # define STDEXEC_CATCH_I (...) { __VA_ARGS__ } } else { __VA_ARGS__ }
547+ # define STDEXEC_TRY (...) if target (nv::target::is_device ) { __VA_ARGS__ } else { try { __VA_ARGS__ }
548+ # define STDEXEC_CATCH (...) catch __VA_ARGS__ }
549+ # define STDEXEC_CATCH_OR (...) catch __VA_ARGS__
535550#else
536- # define STDEXEC_TRY try
537- # define STDEXEC_CATCH (...) catch (__VA_ARGS__) STDEXEC_CATCH_I
538- # define STDEXEC_CATCH_I (...) { __VA_ARGS__ }
551+ # define STDEXEC_TRY (...) try { __VA_ARGS__ }
552+ # define STDEXEC_CATCH (...) catch __VA_ARGS__
553+ # define STDEXEC_CATCH_OR (...) catch __VA_ARGS__
539554#endif
540555// clang-format on
541556
0 commit comments