v.22.5Improvement

Finalize Write Buffers on Exception to Resolve #36907

Finalize write buffers in case of exception to avoid doing it in destructors. Hope it fixes: #36907. #36979 (Kruglov Pavel).
Improve robustness by finalizing write buffers explicitly in case of exceptions instead of relying on destructors.

Why it matters

This change aims to fix issues with improper handling of write buffers during exceptions, which could cause data loss or inconsistent states. By explicitly finalizing write buffers when exceptions occur, it ensures safer and more predictable resource management for users writing data.

How to use it

This improvement is applied internally and does not require special user action. Users benefit from safer write operations automatically after upgrading to the version containing this fix.