C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Bulldoze Exceptions

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 26 Feb 2024 08:24:24 +0000
I reply to Tiago and Jan in series below.


On Sun, Feb 25, 2024 at 3:24 PM Tiago Freire wrote:
>
> If I ever saw such a pattern in production code I would be 99.9% that it was bugged even without looking at what else it is doing.
> Clean up code should never fail.


I've seen lots of clean-up code, or finalisation code, over the years
that can throw exceptions, for example it if opens a file for logging,
or if it writes to a database, or if it sends data out on the RS232
port, or even if it just decrements a semaphore. If one part of the
clean-up or finalisation fails, it's better just to get as much
clean-up or finalisation done as possible (i.e. don't neglect to write
to the database just because the RS232 port failed to write).


On Sun, Feb 25, 2024 at 4:17 PM Jan Schultke wrote:
>
> However, that really makes the feature no more powerful than literally
> wrapping everything in a try {...} catch(...) {} block, and you can
> easily do that with a macro already, or by just writing it out.


Yeah that's right, the following code:

      { _Bulldoze:
          optional<stringstream> ss;
          ss.emplace();
          if ( ss ) *ss << 77;
          if ( ss ) cout << std::move(*ss).str();
      }

is exactly equivalent to:

      { _Bulldoze:
          optional<stringstream> ss;
          try { ss.emplace(); } catch(...){}
          try { if ( ss ) *ss << 77; } catch(...){}
          try { if ( ss ) cout << std::move(*ss).str(); } catch(...){}
      }

This new feature would not make something possible that was previously
impossible, but it would allow programmers to write simpler, neater
code.

Received on 2024-02-26 08:24:36