Professional Documents
Culture Documents
Selected Topics in Computer Programming #4 C++ Exception Safety: Issues and Best Practices
Selected Topics in Computer Programming #4 C++ Exception Safety: Issues and Best Practices
Selected Topics in Computer Programming #4 C++ Exception Safety: Issues and Best Practices
! " # $ ! %
9 10
13 14
W
!"
15 16
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
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
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
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
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
57 58
18>
Selected Topics in Computer Programming #4
! " # $ ! %