0% found this document useful (0 votes)
34 views10 pages

Selected Topics in Computer Programming #4 C++ Exception Safety: Issues and Best Practices

This document discusses best practices for exception safety in C++ programming. It covers three main topics: 1. What should be considered an error and how exceptions should be used appropriately. 2. The importance of program invariants and how they should be maintained before and after exceptions. 3. Taking a big picture view of exception safety and how it improves reliability over time as best practices are developed.

Uploaded by

Oleksiy Kovyrin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views10 pages

Selected Topics in Computer Programming #4 C++ Exception Safety: Issues and Best Practices

This document discusses best practices for exception safety in C++ programming. It covers three main topics: 1. What should be considered an error and how exceptions should be used appropriately. 2. The importance of program invariants and how they should be maintained before and after exceptions. 3. Taking a big picture view of exception safety and how it improves reliability over time as best practices are developed.

Uploaded by

Oleksiy Kovyrin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Selected Topics in Computer Programming #4

Selected Topics in Computer Programming #4 C++ Exception Safety:


Issues and Best Practices

! " # $ ! %

A little about me Best practices improve over time


& $ '( ( ) *+, - ') ( ) ) * & ;1 ! ! & ;> %
& . ! (( . / ) ) ) ) .
& 0 ) # ) ( . ) ! ) )
1 ( -) + % . & ;< ! ! # & ;> %
2 + ! 3( % )) # )
, ! 3( ! (( ! .. . ) ( + ) . ) !
. ( . ) ) (( ) ) )
& $ 1 ( # ) 44 + ( ! % 35- & ;< ! ! & > %
) 6 ! 77 ) ! ! (( ! . ) = ! 0) . ) ?
& ) 77 6 ) # ( ) .
& . # % ! 0 ) 0) )
9 - ,
8 ( ! # ) ( . . ) ! ) )
! (( ! ( ! 9 #
! (( # # :
3 4

Overview What should be considered an error?


& - % . ! ) & $ . % . ) .
) . @ . (. . ! ) ) ' )) !* )
) ) ! (( % ! . @ $ ) ' B ( *.
& 77 0) . ) % . ) D
A ( ) ) ' 03 ( ) 3) ) * . ) ! # ) E # !E )
- 0) 77 # ! ! $ ) ' ( *.
& A ! # 0) . .) E ) % %
5 % '! * . 0) . .) E # . )
0) . 77 # $ % ') ) *.
& 0) . ! ( ! ( ( . ' * # !
< ) B . ) . ! 0) . .. ) ( # D
C . 0) . ! (( ! .) E # ' # *
5 6
Program invariants The big picture
& $ % ' ) )= * ) ( ) & ;< ) ) 0) . (
! ! (E 0 ) ) ) . #L ) ?
F. 0 . ( . ) D & ;$ ) ) ! (( (
< E ! ( ' * ' G) H* D
< # ) % . % #L ) D
I ! ( )J ' K K*+
< ) ) .) ) . #L ) ?
& $) % ) ( #L ) .)
< ) # # #L ) E )E D & ;< ) . 0) . ) . )
) # % ?
, # ( (# ( (# . )
9 -
& % ) . ) . =
$ ( ). ) D
). ) 0 D
, % . #L ) )
7 8

Recommendations re '* About error messages


& ;I ! ) E ! !? & $ . . ( ! = ! . ( .
I '* % . # ) ( ( ;< # .. ! ( = # ! (( )
& '. MM K E ! K *+ # ( . ! (P ) ( ! ?
& ' :N M M K ( # :K *+ & F ;F ! ! ( 8= .
& ' :% ( ' * M M K% ( # ( :K *+ ( # B ( ! % ! ((
& ' #L % ' * M M K #LE % . K *+
;
= ) '*. . .. )
& ;I ! ) ) )= . !
) ! !?
E '*. ) # (
1 # ) .# ) + E # ! ?
9 O> = ( 9 - F Q

9 10

Guidelines for diagnostic messages Techniques to communicate error information


& % 0 ) ) ( ! ! ! ) . . ( # ! .#
I ( = ( = . # )= ' ) *% . 0 ) J
). ) ) # ! 0 ) # (+ % % ! ) . ( ) %
! ! )( ! ' K K* ' . ) J *( = )
& I ( # ! !
R (
$% ## E ) ' *
& $ . ) E J D
I ! (( ) ) ) !
& $ . G J JH
> # ( ( !
( ! !' * F % .. )
& . + % ) ) % %) & I ! 0 J M . ) ( D
< # ( & I ! ( ' ) J
% # # E ! # ( *
I ( ! ) ) (
( ( .= ! F . ' * 77 0)
9O>
11 12
Typical error-
error-checking code What’
What’s right about C++ exceptions
# N . + & E # !
< SS N < S T U+ < S 'S * T U < SS N < S T U+
. ' NN * & $ ( ) ! ) ) ' )= !*
N + & 2 ! . ( ) .
! ( .
. ' = N + = :N + 77 = * . ' = N + = :N + 77 = *
. ' NN ' T=U N <** V T=U N <+ & I # . . ) % . ) E )
N + 3 )
# =+
E ' ! % *
W
( = J ' *+ + F ' ! . *
& % . ) (( ) !
> % ) ) % & % 77 ! ! #
( # % ! = ) ' *

13 14

Reporting failure via a C++ exception Preparing for possible failure


& $ 77 . ) . % 0 & ! . # ! 0)
0 ( 0 N '# NN * @ % J# J ' * 3 #+ ) ( ) 0 ( # )=
V
< ' *
< #L ) ( 0) W
< ' * . 0 % !"

& % !' 0 ) !* 0 & $ # )= ( # . ) E .


X . ) # )= . ( )E
8 0)
' *
1 ) ! 0) . ( 0) #L )
< . !) ') ) ) * #
6 ! E ! ( 3 0) #L ) V

W
!"
15 16

Handling a failure When an exception is thrown


& ) # )= ( ) + & 0 # )= 0)
) ! ( < 0) #L ) D
V W < )= ' ) % # ) * D
) ) ' M * V W $ ) ' # * D
) ) ' ) M * V W < 0) #L ) ' 6
) ) ' * V W $ % &
E ( * D
& $ %) < !% ) 0)
$% ) ) ! # ( ( ( ! ( ) ) !
) # % )= ) 0 & $ 0) #L ) E ( )
$% ) ) !# % % % ) ) # !% )
# ) ' = . 0) ) * 2 ) . )) )
!% . (
( ) ') # 6 # * 0) #L )
17 18

Q
Special situations A handler may exit via
& 8. # )= ( ) ! ! & $ ( 0)
1 ) ) ! # )= < # . ! 0)
< ) ) () ) ! # )= D + ' (
! )= ! $. ) ) 0 ) D
& 8. ( ) ! . ! ( ) 0) #L )
( . ) ( '* ) ! # )=
. ( '*) # '* & $ ( 0) . ..
( . ) ) .
< # ! 0)
( '*# ) ! J ( '*
F. . ) ! ( #
& ( '* ) . 0)
1 ( E ! )= !
1 ( )E E . ) ) #L )
19 20

When should my code throw?


throw? Exceptions have very few performance issues
& $= ! 0 ) 0 ) & ;$ ! ( ( TDU
. ) ! # ) # #L ) % % ! ) )) TDU 0)
Y ( 0 ) # ) ( ) #
) ( # .) ! . ) ?
F %) %
& ;I ! 0) ) ) . ! (
& F. # = ; )= ! @? Z E !(
& , = ;) .. )= ! @? ;T$U) ) # )= ) ) ) ( ) )
) E . ) . % !+ ) # . (
0 ) ( % ! ) . . )
A % 0) . 0)
;T U !Z E !( )
TDU. . % ! . ) ) + !
0) ) ( ( % ?
9 $# (
21 22

Standard exception types Standard exception types in G 0) H


& 0) ) G 0) H & ! )J !) .
< # ) . 0) ! ( # .
8 % ( (# . ) '* % ! ( J
' . ) ) S* % J ! (
& < # ) ! ! ! J
J .J !
# J) G . H
0 ) . % ()) 0 & ( J % #
) . ! ( # .
# J G . H
0 ! J
# J 0) G 0) H % . J
0) ). ) % . J

23 24

/
Thrown by library components Advice for designing your own exception type
& # J ) G H ) & I 0) # ) . 0)
. ) E . ) ! ) ) ' % % J ! ( *
& J# . G H ! > ) ) ) ' 0) M* .) ) ' *
( 83F I % ) % # (# !
& > . ) ( ! 77 [ & < ) )E ( E % .. )
# J. ) J) G. ) H $% ( (# ) )E '
. ) GH #L ) ! !* % .. ) ' J *
# J =J G( ( H ( . # )
0 =J GH & % ! '* = # .(
! 0J G ! 0H . ( ) ( . (
! 0 # ) (
& ! ( (# . ) %
% . ( # E )
25 26

Topics
- % . ! )
Selected Topics in Computer Programming #4
) . @
) ) ! (( % ! . @
C++ Exception Safety: 77 0)
Issues and Best Practices A ( ) ) ' 03 ( ) 3) ) *
- 0) 77 # ! !
& A ! # 0) .
. 5 % '! * . 0) .
0) . 77 #
) # ) B & 0) . ! ( ! ( (
. ! # 0)
< ) B . ) . ! 0) . .. )
. ) % ! 0) . )
C . 0) . ! (( !
28

Reasoning about code in the presence of exceptions “Exception-


Exception-safe code is good for you”
you”
& ; 0) ! & ;T U0) . & ;< ( ) B
) . ! . .. ) ) .) E TDU
! (? 9 X 2 Q ! + % L 0) . )
& $. ) . 0) .. . ! . ! ) # TDU !
0) ) ') * . TDU? ! # ! ?
8( ) ) % # ) . # )= & ;T U0) . & ; 0) . )
0 ) ) % # % ) ) # ! ! )
) ) 9 ! ?
& , ( . ) . 0) . .. . # % (
) ) ) . 0) # ?
0) . B ( . . )
9 2 -
;< E ) ! 0) . (?
9,

29 30
Levels of exception-
exception-safety Design advice
& ) 0) . . ) ! . & ) . ) # ! ) ..
> . $ )) ! ! )
- ! 8) ( )) .. ) Y 6 ) E !
!
) < ! ( ( %
0 ) ! ) . ! ) . ( 5 )= . ! ; )
) ? 9X 2 Q
& < ! ! =
& < # . ) . ) ( ..
# )! D
> ( ! ( ( % D
8. 0 ) ) . 0)

31 32

Ideally, code should never fail Always keep a program in a valid state
& $. ) ) E . ' % * & 8. . ) E # % !
( . ! ! ( ( %
- ) . ) ) % )
& $. ) ( # )! .. .
1 ( ( !
. ( . ! > ) =
& < # 0) . % . ) ) . $ %
. ! . () . ) ) & ;< # )! ( ( ( ( .
1 ( ) E D 0) . ) ) ) ( ?
1 ( ) '*. ) D
8 ) #L ) .)
1 ( ) . ) ) ( )
% ! ) ) #
& < . . ) % ( # ! # )=
#L ) . # ) #
* ) % % D
< . '* % %

33 34

Keep errors from affecting state How to be exception-


exception-unsafe
unsafe [Meyers, 2005]
& 8. # = . . (( = ! % # ) ! & ) V & )E 3 ! E
! ( % & . = . )=' * # D
<S + & 8. 0
& $. ) ( !! .. . + , 0(
( )= D
< . ) .. ) #L ) ( ) ! , 0 (+
# % D
) ) 3) (( # )=3 ! # ) # .. #
( ) % . ' - )M * V
& . % % ) .
)= ' ( *+
& < ! .. ) #L ) % # ) 0) .
+
5 = ) '(*
$ # ) # D 77 +
N < ' *+ A #L ) E
) ( ) # & 8) ' !*
)= ' ( *+
W & I # ' ! !*
W+ & < # = !)
# # ) #
35 36
Advice toward exception-
exception-safety Improving the code 1: address the inconsistency
& ) . . & ) V & - # . . (
% 3 ) ) !
( % D
<S + & C
! ( +
, 0 (+ . ) ( !
& , ! ) % ) A$88
#L ) % % )
& ) % ) ) ) # )
% . ' - )M * V ) ) ) )=
) % # ) (( ' * )= ' ( *+ . E% ) :
) ) )) D + 8. ( )
F % # = # # 77 + ( # )
# )= ' * ) ) ! . )) . N < ' *+ ) ..
77 +
(( % . ) ( % '*. ) )= ' ( *+
W
I % . E ) ) (
W+
37 38

Improving the code 2: address the unusability Improving the code 3: address the resource leak
& ) V & , ! () ! & ) 5 )= V W+ & ) 5 )= V
% % ( ) V % +
<S J G<H + & $ ( E N'* % , 0 M (+
+ 8 . . ) J G<H + # ) - ))
, 0 (+ X ! + 5 )= ' , 0M ) J( *
# ) # . ! ) , 0 (+ ( ') J( *
% . ' - )M * V & F . % # ) + ' , V )= ' ( *+ W
)= ' ( *+ 1 0 % . ' - )M * V ]5 )= ' *
+ % + )= ' ( *+ V )= ' ( *+ W
N < ' *+ F ( N'* 5 )= ! ' ( *+ W+
77 + . ) # ) N < ' *+
)= ' ( *+ 77 +
) E % ( )
W . N'* )= ' ( *+
W+ E # ) : W
W+
39 40

How exception-
exception-safe have we become? Guarantees offered by the C++ standard library
& ) 5 )= V W+ & . 0) & $ (# .. ) ) ! ( 0
) V ! ! 0)
* 0 '* ( '*
% )
J G<H + & . 0) . & $ E ( . ! .
+ !% ! # )! ( . )
, 0 (+
& $) . ( !% * '* % ) GH J# )=' *
# ) ' ,
!! & $. = . ) ( !!
% . ' - )M * V
5 )= ! ' ( *+ ( % # ' )) ) ( % .. ) *
N < ' *+ .. ) ( D * 6 J) '* % ) GH J# )=' *
77 + < .) E )
W & $ ( !. ) ( # )!
0 .
W+ ' % % % ) =*

41 42

\
Writing exception-
exception-safe functions Writing your own exception-
exception-neutral c’
c’tor
& ( ( ! ) ) ( (# . ) & ) V & E )E 0)
) 0) . % . 0)
$ + $E )E D
E ' . ) ( % ) % ! D*
#+ E )E
$ ! ( ') ( % *
# ) & > # #L ) 0 )
E '* 0) % E )E
- : ' * 8. $E )E
& < 0) . #' * # #L ) % )
V W 8. E )E $E E
, (# ' # ( (# * W+ ( ) )
< A$88 # #L )
< ) 8. # . E )E
E E ) $E
< (
& ) #L ) % # !
. ( ) .
43 44

Translating an exception via a function-


function- -block Slight digression: initialization vs. assignment
& ) V & ! % . E )E & $ )#
% 0) 8 6 ! % # E #
$ + & E )E . = ' ( ! % *
#+ $E E )E D $ ! ( ! % # E #
# ) % % . ! E
E #
) # V W+ & <( !
& > # #L ) 0 .3 )
'* ) 8 6 . % # E
)) 0 ) ) ! % # E . (
& % . )
' * $ ! ( ( )) . # B ( ' *
# )= %
#' * ! % # E . (
V W & ) #L ) % # !
& , ) )
) ) ' * . ( ) .
8 6 ) % ! E #
V # ' *+W E E % % ) +
$ ! ( ) 0 !# ! E
W+ #L ) ) ! ! E #
45 46

The copy-
copy-and-
and-swap pattern Applying copy-
copy-and-
and-swap to assignment
& < ! . ( ' . ) * & ! ( .. ! !!
I !) # ) ) -M - N'-) M * V
. #L ) # D - ( ' *+
' ( *+ .%
- ) % ) ) S +
& 8. . . . # D W
& 8. )) % . '* -M - N'- * V . .
' *+
1 . ) ) S +
& !% !! # ) W

< #L ) E .. )) !
& 8. . . ! ( ' NN M *
F= . (6 ' % ( =*
< #L ) E ( ) ) . ) .
. . 8. !) B N'*) E # (
0) .
47 48
Even copy-
copy-and-
and-swap requires care Writing a no-
no-fail move assigment (C++0X)
& % % ) ! . ) ( % & > . '*) L#
.. ) % !) -M - N' - MM * V
& 0 ( –H ' *+
S +
% .' * V W
/ ' % /+
& 1 ( ! ( (# . ( %
!' *+
! ( ( # )
' *+
% -M - N' - MM * V
W N' ( % ' * *+ %-
( N ( % ' ( *+ %-
% . !! !% # ! # D % '
.( # # !% !! # ) D S +
.) E ) ! .) E ! . !E .. ) W
) 0)
49 50

Obtaining a no-
no-fail '* Writing a strong copy c’
c’tor
& A ( (# . '* & I ( (# ) 6
% - '-M * V % - -' - ) M *
' *+ '* ' *
( ' ( *+ '* ( ' ( *
'( ( *+ V W
W & ) # ) ( (# .-( . % !
& ) # 3( (# .-( .# # ' . *) )E
) ( % . '* D 2 ' ) ( ! * % # !
( '* E . > % % B % ) =
& A B . ) 3( % & ( ! ) )B % %
& 8. ( (# # 0 ) ) ) )
) !( . ) @
) % '( *
* # ( (# ) @ ) @
) 3 . #
< ) % % ) )E E # E
51 52

More copy-
copy-and-
and-swap: the pimpl pattern Writing a no-
no-fail move c’
c’tor (C++0X)
) V # ) + ' , & I ( (# . ( % 6
% ) . )
) ( V % ' * V
% - -' - M M *
% J ' ( ' S * *+
' ( % ' **
H ( J. ' *+
( ' ( % ' ( **
# ) + .% ' *+
V
W
N -' *+ %
% ( J. ' *+ W+
W
W+
& A % # . ) .
.
' % . . )E *
J G( H
J+ > . ) ( !
J +

53 54

4
Writing a no-
no-fail d’
d’tor General principles of exception safety
& , E . ) # )= & > % ! . ) . . ( # .
$. ) # )= 0 % D ) ( . % #
< . . & $ % #L ) % !
& 8 ) 0) % # )= ! 0)
) '* & 8 . ! . (
] '* V ) E D
V
/ ) ) ) ( . ) D
W ) . )
) ) ' * V
' ' ' )'
& C% ) . ) 3 ) ) #
.
W
W
55 56

Mechanisms underlying exception-


exception-safe code Summary
& < # ) ) & ;, ) ) ) ! #
< ! ! E # )= D ! ! ) . # 9 ( =
) 0 ) . ) ?
< # E '*
& ;8 ! E 0) ( ! . )
& 8( ) ! ) B P ( ! ) ! <
< ) )B 6 # . ! ! ?
, (# ' 3# 3( (# * & ;I 0) . !
< ) A ) % !. ( %
< ( 3 % 3 -! ! # ) . ) ) )
A ! . E # ) ?
9 )= \

57 58

18>
Selected Topics in Computer Programming #4

C++ Exception Safety:


Issues and Best Practices

! " # $ ! %

You might also like