finish_or_die.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #pragma once
  2. #include <util/system/yassert.h>
  3. #include <exception>
  4. /// @cond Doxygen_Suppress
  5. namespace NYT::NDetail {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. template <typename T>
  8. void FinishOrDie(T* pThis, bool autoFinish, const char* className) noexcept
  9. {
  10. if (!autoFinish) {
  11. return;
  12. }
  13. auto fail = [&] (const char* what) {
  14. Y_ABORT(
  15. "\n\n"
  16. "Destructor of %s caught exception during Finish: %s.\n"
  17. "Some data is probably has not been written.\n"
  18. "In order to handle such exceptions consider explicitly call Finish() method.\n",
  19. className,
  20. what);
  21. };
  22. try {
  23. pThis->Finish();
  24. } catch (const std::exception& ex) {
  25. if (!std::uncaught_exceptions()) {
  26. fail(ex.what());
  27. }
  28. } catch (...) {
  29. if (!std::uncaught_exceptions()) {
  30. fail("<unknown exception>");
  31. }
  32. }
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////
  35. } // namespace NYT::NDetail
  36. /// @endcond