5.2.30 · HinglishC++ Programming

noexcept specifier

1,748 words8 min readRead in English

5.2.30 · Coding › C++ Programming


YEH HAI KYA?

void a() noexcept;          // promises: never throws
void b() noexcept(true);    // same thing
void c() noexcept(false);   // may throw (this is also the default)
void d();                   // may throw (default)

YEH EXIST KYUN KARTA HAI?

Purana C++98 throw() wahi kaam karta tha lekin clunky tha aur ab deprecated/removed hai. noexcept ne ise replace kiya.


YEH BEHAVE KAISE KARTA HAI? (consequences derive karo)

Hum rules memorize nahi karte — hum unhe derive karte hain ek single contract se: "koi bhi exception escape nahi ho sakti."

Terminate kyun, rethrow kyun nahi? Kyunki poora point yahi tha ki compiler is function ke liye exception-handling tables skip kar sake. Literally koi unwinding info use karne ke liye ho hi nahi sakti — toh ek hi safe action hai: sab kuch rok do.

Yeh hume conditional noexcept likhne deta hai:

template<class T>
void swap(T& a, T& b) noexcept( noexcept(T(std::move(a))) &&
                                noexcept(a = std::move(b)) );

Yahan outer noexcept(...) specifier hai; inner noexcept(...) woh operator hai jo bool compute karta hai. "Main noexcept hoon exactly tab jab T ko move karna throw nahi kar sakta."

Figure — noexcept specifier

Worked examples


Common mistakes


Recall Feynman: ek 12-saal ke bachche ko samjhao

Socho tum apne teacher ko bolte ho: "MAIN PROMISE KARTA HOON ki is kamre ko cross karte waqt paani ka glass nahi giraunga." Agar tum promise nibhao, teacher relax karta hai aur har jagah towels aur mops nahi bikhata — time aur effort bachaata hai. Lekin agar tumne GIRADIYA promise karne ke baad? Tumhe sirf daanta nahi jaata — poori class turant ghar bhej di jaati hai, koi sawaal nahi. noexcept woh promise hai exception "spillage" ke baare mein. Nibhao aur program lean chalega; todo aur program turant band ho jaata hai.


Recall flashcards

noexcept specifier kya promise karta hai?
Ki function kisi bhi exception ko apne se bahar (propagate) nahi hone dega.
Agar ek noexcept function exception escape hone de toh kya hota hai?
std::terminate() turant call hota hai; program kill ho jaata hai (koi guaranteed unwinding nahi).
noexcept kis cheez ka shorthand hai?
noexcept(true) ka.
Ek ordinary function ki default exception specification kya hai?
noexcept(false) — yeh throw kar sakta hai.
noexcept(expr) operator kya return karta hai?
Ek compile-time bool: true agar expr guaranteed non-throwing ho, warna false. expr unevaluated hota hai.
std::vector kyun care karta hai ki kisi type ka move constructor noexcept hai ya nahi?
Agar move noexcept hai toh reallocation par elements move karta hai (fast); warna strong exception guarantee preserve karne ke liye copy karta hai (slow).
noexcept specifier aur noexcept operator mein kya fark hai?
Specifier (signature ke baad) ek promise karta hai; operator noexcept(x) bool evaluate karta hai. Same keyword, do roles.
C++11 se destructors kya by default noexcept hain?
Haan — implicitly noexcept; unse throw karna terminate call karta hai.
Kya ek noexcept function ke andar exception throw aur catch karna contract violate karta hai?
Nahi — sirf woh exception jo function se escape kare woh terminate trigger karti hai.
noexcept ne kis purani cheez ko replace kiya?
C++98 dynamic exception specification throw() ko (ab deprecated/removed).
T ke move ke basis par conditional noexcept likho.
void f() noexcept(noexcept(T(std::move(t))));

Connections

  • Exception handling in C++try/catch/throw mechanics jis par yeh build karta hai
  • Move semantics — kyun noexcept move ctors fast reallocation unlock karte hain
  • std::vector — growth ke dauran move_if_noexcept use karta hai
  • Strong exception guarantee — woh safety level jise noexcept maintain karne mein help karta hai
  • Destructors — C++11 se implicitly noexcept
  • std::terminate — jo broken promise par fire hota hai
  • constexpr — noexcept-operator aur constexpr dono compile-time evaluated hain

Concept Map

has two forms

has two forms

declares

if violated

yields

enables

enables

so vector can

keeps

deprecated, replaced by

unevaluated like sizeof

noexcept C++11

Contract: never throws

Specifier: f noexcept

Operator: noexcept expr

Compile-time bool

std::terminate

Performance

vector prefers move

Strong exception guarantee

Conditional noexcept

C++98 throw