5.2.30 · D1 · HinglishC++ Programming

Foundationsnoexcept specifier

1,847 words8 min read↑ Read in English

5.2.30 · D1 · Coding › C++ Programming › noexcept specifier

Isse pehle ki tum parent note the `noexcept` specifier par trust karo, tumhe dekhna hoga ki ek exception actually kya hoti hai, "ek function se escape karna" kaisa dikhta hai, aur bool, template, aur std::move jaise chote words ka kya matlab hai. Yeh page unhe har ek ko zero se build karta hai, us order mein jisme woh ek dusre ke upar stack hote hain.


0 · Ek function call kya hota hai, stack ke roop mein draw kiya?

noexcept ki har baat is baare mein hai ki jab cheezein function calls ke beech galat hoti hain toh kya hota hai. Isliye humein pehle calls ko picture karna hoga.

Figure — noexcept specifier

Frames ke tower ko dekho. Normal return = top plate ko pop karo aur neeche continue karo. Exceptions ki poori story yeh hai: kya hoga agar hume ek error ki wajah se ek saath kaafi plates remove karni hon?


1 · Exception kya hoti hai, aur "escape" ka matlab kya hai?

Figure — noexcept specifier

Figure mein, amber path follow karo. b throw karta hai. b mein koi jaal nahi hai, isliye b ka frame destroy ho jaata hai aur signal a mein drop ho jaata hai. a mein bhi koi jaal nahi — a ka frame bhi destroy ho jaata hai. Finally main mein ek catch hai, isliye signal wahan catch ho jaata hai.

Yeh word escape noexcept ka dil hai. Promise precisely yeh hai: "ek exception kabhi mujhse bahar nahi jayegi." Yeh nahi ki "main internally kabhi throw nahi karta" — sirf yeh ki kuch leaves nahi hoga.


2 · Destructors — woh cleanup jo unwinding ke dauran chalta hai

Yeh woh danger hai jis par parent hint karta hai. Unwinding ke dauran hum pehle se ek exception carry kar rahe hain. Agar ek destructor bhi mid-unwinding throw kare, toh ab hamare paas ek saath do exceptions travel kar rahe hain — runtime choose nahi kar sakta ki kis ko follow kare, isliye language kehti hai: yeh illegal hai, std::terminate call karo. Isliye Destructors C++11 se implicitly noexcept hain.

Poore throw/catch mechanics ke liye Exception handling in C++ dekhein.


3 · std::terminate — kill switch


4 · bool aur compile-time — noexcept(expr) kya produce karta hai

Parent noexcept ko ek doosre role mein use karta hai: ek operator ki tarah jo ek bool return karta hai. Do words unlock karne ke liye.

Kyunki yeh ek compile-time bool hai, tum ise ek constexpr variable mein store kar sakte ho aur generic code mein iske upar branch kar sakte ho.


5 · template<class T> — ek type-shaped hole wala code

Humhe templates isliye chahiye kyunki ek function ko noexcept hona chahiye ya nahi yeh often T par depend karta hai — saste ints ka swap kabhi throw nahi karta, lekin ek throw-prone type ka swap kar sakta hai. Isliye conditional noexcept.


6 · std::move aur move semantics — kyun vector care karta hai

Figure — noexcept specifier

Prerequisite map

Call stack and frames

Exceptions throw and catch

Stack unwinding and escape

Destructors run during unwinding

std terminate kills on broken promise

bool and compile time

noexcept operator returns bool

templates fill a type hole

conditional noexcept

copy vs move semantics

vector move or copy choice

noexcept specifier

Har arrow ka matlab hai "left box ko samajhna zaroori hai pehle right box make sense kare." Sare raaste bottom mein parent topic ko feed karte hain.


Equipment checklist

Khud ko test karo — sirf tab reveal karo jab tum zor se jawab de chuke ho.

A stack frame is
ek box jo ek function call ki local variables hold karta hai; calls ek dusre ke upar stack hote hain.
"Stack unwinding" means
frames ko ek ek karke destroy karna jab ek exception neeche matching catch dhundhte hue drop hoti hai.
An exception "escapes" a function when
woh function ko bina andar catch hue chhodke chali jaati hai (sirf yahi noexcept penalty trigger karta hai).
std::terminate does
program ko turant khatam kar deta hai bina kisi guaranteed further cleanup ke.
A bool is
ek value jo ya toh true ya false hoti hai.
"Compile-time" vs "run-time"
compile-time tab hota hai jab program build ho raha hota hai (run hone se pehle); run-time tab hota hai jab woh run ho raha hota hai.
noexcept(expr) as an operator evaluates expr how
yeh unevaluated hai — compiler isse throwing ke liye inspect karta hai lekin kabhi run nahi karta, ek compile-time bool return karta hai.
template<class T> is
ek recipe jisme ek type-shaped placeholder T hai, har concrete type ke liye ek real function mein stamp hota hai.
Difference between copy and move
copy duplicate karta hai (original intact); move internals steal karta hai (original gutted) — sasta.
std::vector prefers moving on reallocation only when
move constructor noexcept ho, warna woh strong exception guarantee rakhne ke liye copy karta hai.