c++ - Confusion around noexcept -


this question has answer here:

after watching many videos, reading book, unclear when , when not use noexcept.

all of books should use noexcept when function never ever throw.

i think should used otherwise. many functions allocate shouldn't noexcept, if don't want catch errors, , call std::terminate acceptable?

in short, should noexcept used on functions never throw, or on functions except ones catch exceptions from.

imho exceptions don't need caught (ie out of memory etc)

the marker noexcept gurantee developer compiler function never throw.

so should add functions know should never throw. if these functions obscure , unknowable reason throw thing compiler can terminate application (as guaranteed should not happen). note: function marked noexcept should not call function unless marked noexcept (just const correctness need have noexcept correctness)

where should use it:

  swap()   methods/functions.             swap supposed exception safe.            lots of code works on assumption.    move constructor   move assignment.            object moving should            formed moving processes of            swapping things around.             marking them noexcept there             optimizations in standard library can used.       note: case.            if can not guarantee move/swap semantics             exception safe not mark functions noexcept. 

you don't want call terminate on exceptions. of time allow exception unwind stack calling destructors , releasing resources correctly.

it not caught application terminate.

but complex applications should resilient exceptions. catch discard initiated task log exception , wait next command. still want exit because smudge operation in graphics application fails not mean want application exit ungracefully. rather smudge operation abandoned resource re-claimed , application goes normal operations (so can save exit , restart).


Comments