Skip to content

Commit 1ecbdd1

Browse files
Document that try/finally expressions take the value of the try block (#57663)
Close #56855
2 parents b1fffb0 + 11bcf79 commit 1ecbdd1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

base/docs/basedocs.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,41 @@ normally), [`close(f)`](@ref) will be executed. If the `try` block exits due to
11181118
the exception will continue propagating. A `catch` block may be combined with `try` and
11191119
`finally` as well. In this case the `finally` block will run after `catch` has handled
11201120
the error.
1121+
1122+
When evaluating a `try/catch/else/finally` expression, the value of the entire
1123+
expression is the value of the last block executed, excluding the `finally`
1124+
block. For example:
1125+
1126+
```jldoctest
1127+
julia> try
1128+
1
1129+
finally
1130+
2
1131+
end
1132+
1
1133+
1134+
julia> try
1135+
error("")
1136+
catch
1137+
1
1138+
else
1139+
2
1140+
finally
1141+
3
1142+
end
1143+
1
1144+
1145+
julia> try
1146+
0
1147+
catch
1148+
1
1149+
else
1150+
2
1151+
finally
1152+
3
1153+
end
1154+
2
1155+
```
11211156
"""
11221157
kw"finally"
11231158

doc/src/manual/control-flow.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,41 @@ When control leaves the `try` block (for example due to a `return`, or just fini
893893
continue propagating. A `catch` block may be combined with `try` and `finally` as well. In this
894894
case the `finally` block will run after `catch` has handled the error.
895895

896+
When evaluating a `try/catch/else/finally` expression, the value of the entire
897+
expression is the value of the last block executed, excluding the `finally`
898+
block. For example:
899+
900+
```jldoctest
901+
julia> try
902+
1
903+
finally
904+
2
905+
end
906+
1
907+
908+
julia> try
909+
error("")
910+
catch
911+
1
912+
else
913+
2
914+
finally
915+
3
916+
end
917+
1
918+
919+
julia> try
920+
0
921+
catch
922+
1
923+
else
924+
2
925+
finally
926+
3
927+
end
928+
2
929+
```
930+
896931
## [Tasks (aka Coroutines)](@id man-tasks)
897932

898933
Tasks are a control flow feature that allows computations to be suspended and resumed in a flexible

0 commit comments

Comments
 (0)