Huge count of exception catches

In TransformerEncoder::TransformerEncoder func present code like

for (size_t l = 0;; ++l) {
        const std::string layer_scope = scope + "/layer_" + std::to_string(l);
        try {
          auto layer = std::make_unique<TransformerEncoderLayer>(model,
                                                                 layer_scope,
                                                                 num_heads,
                                                                 pre_norm,
                                                                 activation_type);
          _layers.emplace_back(std::move(layer));
        } catch (std::exception&) {
          if (l == 0)
            throw;
          else
            break;
        }
      }

look like hardcoded hack, can anyone explane to me what is this exception catch processing, and what can i do for decrease or forget about this exceptions calls

exception view

Exception thrown at 0x00007FFD5D134F69 in test.exe: Microsoft C++ exception: std::out_of_range at memory location 0x0000004F2BEFD8E0.

thx.

The number of layers is not known earlier, so the code tries to build a layer and stops as soon as it fails with an exception. I agree this not clean and I want to change that.

Is this causing an actual issue in your project?

This is not critical issue, just create more trash in app exceptions log.
after your explain i have no questions, but i think this code need in more pretty solution )

thx

I’m updating the code to not rely on these exceptions:

Thanks for the feedback.