file f("file.txt");
What happens if the file does not exist?
file f;bool bOk=f.open("file.txt");if( !bOk ) {...}
out parameter
bool bOk;file f("text.txt",bOk);if( !bOk ) {...}
clutter code with checks
[[nodiscard]]
for return valuesstatus: bad flag on first failure
good if checking at the very end is good enough
default for C++ iostreams
std::variant<result, error>
+ utilitiesstd::expected
auto inc(int i)->int { // throw(char const*) if(3==i) throw "Hello"; return i+1;}auto main()->int { try { int n=3; n=inc(n); // throw(char const*) } catch( char const* psz ) { std::cout << psz; } return 0;}
auto inc(int i)->int { // throw(char const*) if(3==i) throw "Hello"; return i+1;}auto main()->int { try { int n=3; n=inc(n); // throw(char const*) } catch( char const* psz ) { std::cout << psz; } return 0;}
(not really exception-specific)
Part of function specification
Never Fails
Strong Exception Guarantee:
(not really exception-specific)
Part of function specification
Never Fails
Strong Exception Guarantee:
Basic Exception Guarantee:
GetLastError()
, HRESULT
errno
assert
aggressivelynoexcept
if caller does not handle exceptionstd::terminate
, but unexpected exceptions will terminate anywaystd::set_terminate
for checkingGetLastError()
, HRESULT
errno
assert
aggressivelynoexcept
if caller does not handle exceptionstd::terminate
, but unexpected exceptions will terminate anywaystd::set_terminate
for checkingGetLastError()
, HRESULT
errno
assert
aggressivelynoexcept
if caller does not handle exceptionstd::terminate
, but unexpected exceptions will terminate anywaystd::set_terminate
for checkingassert
s can be wrong, toonullptr
accessServer: notify operator, enter infinite loop (wait for debugger)
Notify user only if false alarm unlikely
assert
s may be wrongauto RegisterFooHook(Foo foo) { errcode_t err=RegisterFoo(foo); if(err==SUCCESS) KeepTrackOfFoo(foo); return err;}
err
indicates error, does nothing, no error handling neededRegisterFoo
failingEffect on rest of the program?
Client: send report, throttle future reports
Report database with all reports
0 1 1 0 0 1 0 1 0 1 1 0
(6 occurrences among 12 reports)
Report database with all reports
0 1 1 0 0 1 0 1 0 1 1 0
(6 occurrences among 12 reports)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
Report database with all reports
0 1 1 0 0 1 0 1 0 1 1 0
(6 occurrences among 12 reports)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
0 1 1 0 0 1 0 1 0 1 1 0
(6/12)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
p
that report has particular problemall p
in [0,1]
equally likely
no. bits to compress N
bits with K
ones:
log [ (N+1) * (N over K) ]
0 0 1 0 0 1 0 1 0 1 1 0
(6/12)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
0 0 1 0 0 1 0 1 0 1 1 0
(6/12)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
0 0 1 0 0 1 0 1 0 1 1 0
(6/12)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
- x x - x x - x - x - -
Module C (with: 5/6, without: 1/6)
0 0 1 0 0 1 0 1 0 1 1 0
(6/12)
x - x - - x x - x - x -
Module A (with: 3/6, without: 3/6)
- x x - x x - x x - - -
Module B (with: 4/6, without: 2/6)
- x x - x x - x - x - -
Module C (with: 5/6, without: 1/6)
Example: int arithmetic overflow
If program contains undefined behavior, compiler can do anything with the whole program!
int i=...;int j=i;++i;// code for overflow may be thrown out by compiler!// if( i<j ) {// ... treat overflow ...// }
unsigned
types are different: modulo arithmeticKeyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |