5.2.29 · D3 · HinglishC++ Programming

Worked examplesException safety — basic, strong, no-throw guarantees

3,714 words17 min read↑ Read in English

5.2.29 · D3 · Coding › C++ Programming › Exception safety — basic, strong, no-throw guarantees


The scenario matrix

Jaise ek angle formula mein quadrants hote hain jahan naive version fail karta hai, exception safety mein bhi "cells" hote hain — recurring situations jo alag-alag answer maangti hain. Neeche ki figure hamara board hai: har cell ek square hai, colour se pata chalta hai ki kaunsa tool usse fix karta hai, bilkul waise jaise angle ke chaar quadrants chart karte ho.

Figure — Exception safety — basic, strong, no-throw guarantees
The scenario board. Har square ek cell hai (C1–C9); uska colour batata hai kaunsa tool apply hota hai.

Yeh pura board words mein:

Cell Situation Core question
C1 Single resource, acquire ke baad throw ho sakta hai Kya cleanup phir bhi hoga?
C2 Do resources, doosra acquisition (allocation) throw karta hai Kya pehla leak hoga?
C3 Multi-step visible mutation, baad wala step throw karta hai Kya pehle wale steps roll back honge?
C4 C3 ka copy-and-swap upgrade Kya hum isse strong bana sakte hain?
C5 "Zero / degenerate" input (empty, self, no-op) Kya safe path trivial input survive karta hai?
C6 Move jo mid-transfer throw karta hai Kya strong guarantee kho jaati hai? Copy vs move.
C7 noexcept jo jhoot bole aur throw kare Runtime pe actually kya hota hai?
C8 Real-world word problem (bank transfer) Business ko kaunsi guarantee chahiye?
C9 Exam twist: kaunsi guarantee, aur kyun stronger nahi? Pure picture ke baare mein soch, parts ke baare mein nahi.

Neeche, har example cell(s) ke saath tagged hai. Milake ye pura board cover karte hain.


Example 1 — Single resource (Cell C1)


Example 2 — Do resources, doosra throw karta hai (Cell C2)


Example 3 — Multi-step visible mutation (Cell C3)


Example 4 — C3 ko strong banao (Cell C4)


Example 5 — Degenerate inputs (Cell C5)


Example 6 — Move jo throw karta hai (Cell C6)


Example 7 — Ek noexcept jo jhooth bole (Cell C7)


Example 8 — Real-world word problem (Cell C8)


Example 9 — Exam twist (Cell C9)


Recall Kaunse cell ko kaunsa tool chahiye?

C1/C2 → RAII / Smart pointers — unique_ptr, shared_ptr. C3→C4 → Copy-and-swap idiom. C6 → noexcept moves + std::vector reallocation strategy. C7 → honest noexcept. C8 → domain invariants ke liye strong. C9 → whole = weakest path.

Active recall

Bank transfer kyun strong chahiye, basic nahi?
Kyunki basic objects ko valid rakhta hai lekin debit ko persist hone deta hai jabki credit fail ho — money conserved nahi hoti; domain invariant broken hai.
Example 2 mein exactly kitne Widgets leak hote hain aur kyun?
Ek — a ka raw pointer koi destructor nahi rakhta, toh unwinding kabhi isse free nahi karti jab b ka new throw karta hai.
Copy-and-swap self-assignment-safe automatically kyun hai?
Right-hand copy pass-by-value se body run hone se pehle banti hai, toh *this aur rhs hamesha alag objects hote hain.
std::vector kya karta hai jab T ka move constructor noexcept nahi hota?
Woh reallocation ke dauran elements copy karta hai (move_if_noexcept), kyunki ek throwing move source ko gut kar deta aur strong guarantee kho deta.
Multi-path function ki whole-function guarantee kya hoti hai?
Sare reachable paths mein sabse kamzor guarantee.