From 464c93e338baba01ba89423c4734603ed2d7d4eb Mon Sep 17 00:00:00 2001 From: Simeon David Schaub Date: Thu, 26 Jan 2023 13:56:07 +0100 Subject: [PATCH 1/2] add docs for try-catch-else ref #46928 --- doc/src/manual/control-flow.md | 38 +++++++++++++++++++++++++ doc/src/manual/variables-and-scoping.md | 2 +- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/doc/src/manual/control-flow.md b/doc/src/manual/control-flow.md index 18a84957fe625..0892cc33f8c21 100644 --- a/doc/src/manual/control-flow.md +++ b/doc/src/manual/control-flow.md @@ -827,6 +827,44 @@ no error has occurred, but the ability to unwind the stack and pass a value to a is desirable. Julia provides the [`rethrow`](@ref), [`backtrace`](@ref), [`catch_backtrace`](@ref) and [`current_exceptions`](@ref) functions for more advanced error handling. +### `else` Clauses + +!!! compat "Julia 1.8" + This functionality requires Julia 1.8. + +In some cases, one may not only want to appropriately handle the error case, but also want to run +some code only if the `try` block succeeds. For this, an `else` clause can be specified after the +`catch` block that is run whenever no error was thrown previously. The advantage over including +this code in the `try` block instead is that any further errors don't get silently caught by the +`catch` clause. + +```julia +local x +try + x = read("file", String) +catch + # handle read errors +else + # do something with x +end +``` + +!!! note + The `try`, `catch`, `else`, and `finally` clauses each introduce their own scope blocks, so if + a variable is only defined in the `try` block, it can not be accessed by the `else` or `finally` + clause: + ```jldoctest + julia> try + foo = 1 + catch + else + foo + end + ERROR: UndefVarError: `foo` not defined + ``` + Use the [`local` keyword](@ref local-scope) outside the `try` block to make the variable + accessible from anywhere within the outer scope. + ### `finally` Clauses In code that performs state changes or uses resources like files, there is typically clean-up diff --git a/doc/src/manual/variables-and-scoping.md b/doc/src/manual/variables-and-scoping.md index ebb4559b3e854..8bd62fe7ee5bf 100644 --- a/doc/src/manual/variables-and-scoping.md +++ b/doc/src/manual/variables-and-scoping.md @@ -111,7 +111,7 @@ x = 1 Note that the interactive prompt (aka REPL) is in the global scope of the module `Main`. -## Local Scope +## [Local Scope](@id local-scope) A new local scope is introduced by most code blocks (see above [table](@ref man-scope-table) for a complete list). If such a block is syntactically nested From 82695d3dbed083de515ffc3aa7316e6799a31663 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Thu, 26 Jan 2023 20:34:41 +0100 Subject: [PATCH 2/2] Update doc/src/manual/control-flow.md Co-authored-by: Ian Butterworth --- doc/src/manual/control-flow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/manual/control-flow.md b/doc/src/manual/control-flow.md index 0892cc33f8c21..ba78a8c5b1670 100644 --- a/doc/src/manual/control-flow.md +++ b/doc/src/manual/control-flow.md @@ -830,7 +830,7 @@ and [`current_exceptions`](@ref) functions for more advanced error handling. ### `else` Clauses !!! compat "Julia 1.8" - This functionality requires Julia 1.8. + This functionality requires at least Julia 1.8. In some cases, one may not only want to appropriately handle the error case, but also want to run some code only if the `try` block succeeds. For this, an `else` clause can be specified after the