Isse pehle ki tum "basic vs strong vs no-throw" ke baare mein soch sako, tumhe kuch building blocks chahiye jo parent note ne quietly assume kar liye the. Hum inhe ek-ek karke zero se banate hain: plain words → ek picture → kyun is topic ko yeh chahiye.
Figure dekho. main bottom pe baitha hai. Usne f ko call kiya, jisne ek box upar stack ki; f ne g ko call kiya, ek aur box; g ne new (memory allocation) ko call kiya, woh top red box. Abhi sirf top wali box run kar sakti hai. Neeche sab kuch paused hai, apne upar wali box ke khatam hone ka intezaar kar raha hai.
Word throw literal hai: socho top box ek ball seedha neeche stack mein toss kar rahi hai, box by box, kisi aisa dhundhte hue jo use pakadne ke liye raazi ho (ek try/catch). Jab tak pakda nahi jaata, har woh box jisse woh guzarti hai destroy ho jaati hai.
Figure mein, red exception g se main ki taraf upar jaati hai. Jaise hi woh har box se guzarti hai, us box ke destructors fire hote hain (chhote marks). Yahi C++ tumhe guaranteed cleanup ke liye ek mechanism deta hai — aur yeh har us guarantee ki foundation hai jise hum study karenge.
Figure mein pairing dikhti hai: jis waqt object janam leta hai (top), woh resource grab karta hai; jis waqt uski box pop hoti hai (bottom), destructor resource wapas kar deta hai. Koi gap nahi jahan se exception nikal ke cleanup skip kar sake.
new (acquire) likhne aur ek matching delete (release) yaad rakhne ki — jo unwinding skip kar sakta hai — jagah, tum ek baar std::make_unique<Widget>() likhte ho aur destructor delete guarantee karta hai. Yahi RAII hai most common resource pe apply hota hua: memory. Dekho Smart pointers — unique_ptr, shared_ptr.
Figure dekho: a aur b dono ek buffer ki taraf pointer hold karte hain. Swap bas re-label karta hai ki kaun sa pointer kiske paas hai — koi memory copy nahi hoti, kuch run out nahi ho sakta. Kyunki yeh throw nahi kar sakta, swap last, safe instant hai changes commit karne ka. Yahi Copy-and-swap idiom ka dil hai.
Call stack kis cheez ka stack hai, aur kis order mein add aur remove hoti hain? ::: Function frames (locals ki boxes) ka stack, top-first add aur remove hoti hain (last-in first-out).
throw function ke normal flow ko kya karta hai? ::: Woh function ko turant abandon karta hai aur normally return karne ki jagah ek exception object ko stack ke upar bhejta hai.
Stack unwinding kya hai? ::: Runtime frames ko top se ek ek karke pop karta hai jab tak catch na mile, destructors run karta hai jaise har frame pop hoti hai.
Destructor exactly kab run hota hai? ::: Us instant jab object ka frame pop hota hai — chahe normal scope exit se ho ya unwinding se.
Ek resource, ek leak, aur ek invariant define karo. ::: Resource = kuch jo tum acquire karte ho aur release karna padta hai; leak = acquire kiya par kabhi release nahi kiya; invariant = ek rule jo object ke baare mein hamesha hold karna chahiye.
RAII resource ki lifetime ko kya se glue karta hai? ::: Ek stack object ki lifetime se — constructor mein acquire, destructor mein release.
std::unique_ptr apni owned memory ke baare mein kya guarantee deta hai? ::: Uska destructor delete automatically run karta hai jab pointer ka frame pop hota hai, toh memory free ho jaati hai chahe unwinding ke dauran bhi.
Agar ek noexcept function actually throw kare toh kya hota hai? ::: Runtime std::terminate() call karta hai aur program bina unwinding ke turant mar jaata hai.
Pointer-holding objects ko swap karna kabhi throw kyun nahi kar sakta? ::: Yeh bas ek pointer aur ek size exchange karta hai — kuch assignments, koi allocation nahi, kuch bhi fail nahi ho sakta.
std::vector move ki jagah copy kyun karta hai jab type ka move noexcept nahi hota? ::: Ek throwing move purane buffer ko halfway gut kar sakta hai aur strong guarantee destroy kar sakta hai, toh woh source intact rakhne ke liye copy karta hai.