From 0cf4b8bb92848d267980b9995bdffa99d59df53b Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Thu, 2 Oct 2025 15:28:33 -0400 Subject: [PATCH 1/4] Add `rethrow` to example in Exception Handling --- doc/src/manual/control-flow.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/doc/src/manual/control-flow.md b/doc/src/manual/control-flow.md index 231ec53741111..5f1506a5789f5 100644 --- a/doc/src/manual/control-flow.md +++ b/doc/src/manual/control-flow.md @@ -785,6 +785,8 @@ julia> sqrt_second(x) = try sqrt(complex(x[2], 0)) elseif isa(y, BoundsError) sqrt(x) + else + rethrow() # ensure other exceptions can bubble up the call stack end end sqrt_second (generic function with 1 method) @@ -803,8 +805,18 @@ ERROR: DomainError with -9.0: sqrt was called with a negative real argument but will only return a complex result if called with a complex argument. Try sqrt(Complex(x)). Stacktrace: [...] + +julia> sqrt_second([1 nothing]) +ERROR: MethodError: no method matching sqrt(::Nothing) +The function `sqrt` exists, but no method is defined for this combination of argument types. +[...] ``` +Use `rethrow()` as above to continue unwinding the stack with the original exception so that +higher-level exception handlers can deal with the exception. When filtering by exception type +as above, it is often important to include `else rethrow()` so that other types of exceptions +are not hidden from the caller. + Note that the symbol following `catch` will always be interpreted as a name for the exception, so care is needed when writing `try/catch` expressions on a single line. The following code will *not* work to return the value of `x` in case of an error: From 87029a0c1c5264a8d5fb40f2ae5cca82f438653c Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Thu, 2 Oct 2025 15:36:29 -0400 Subject: [PATCH 2/4] Add demonstration of filtering/rethrow to docstrings --- base/docs/basedocs.jl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index 08c8aa0fb2357..f9441b5be4368 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -1063,6 +1063,20 @@ end The syntax `catch e` (where `e` is any variable) assigns the thrown exception object to the given variable within the `catch` block. +```julia +try + a_dangerous_operation() +catch e + if isa(e, EOFError) + @warn "The operation failed - EOF." + elseif isa(e, IOError) + @warn "The operation failed - IO Error." + else + rethrow() # ensure other exceptions can bubble up the call stack + end +end +``` + The power of the `try`/`catch` construct lies in the ability to unwind a deeply nested computation immediately to a much higher level in the stack of calling functions. From 193937b2b3b4ed791211cc990816fc3ecb557139 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Thu, 2 Oct 2025 15:39:20 -0400 Subject: [PATCH 3/4] Move reference --- doc/src/manual/control-flow.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/manual/control-flow.md b/doc/src/manual/control-flow.md index 5f1506a5789f5..04bb1ea621f33 100644 --- a/doc/src/manual/control-flow.md +++ b/doc/src/manual/control-flow.md @@ -812,7 +812,7 @@ The function `sqrt` exists, but no method is defined for this combination of arg [...] ``` -Use `rethrow()` as above to continue unwinding the stack with the original exception so that +Use [`rethrow`](@ref) as above to continue unwinding the stack with the original exception so that higher-level exception handlers can deal with the exception. When filtering by exception type as above, it is often important to include `else rethrow()` so that other types of exceptions are not hidden from the caller. @@ -839,7 +839,7 @@ end The power of the `try/catch` construct lies in the ability to unwind a deeply nested computation immediately to a much higher level in the stack of calling functions. There are situations where no error has occurred, but the ability to unwind the stack and pass a value to a higher level -is desirable. Julia provides the [`rethrow`](@ref), [`backtrace`](@ref), [`catch_backtrace`](@ref) +is desirable. Julia provides the [`backtrace`](@ref), [`catch_backtrace`](@ref) and [`current_exceptions`](@ref) functions for more advanced error handling. ### `else` Clauses From 829bef00c942897cf476d41e528d9e46b6c257d7 Mon Sep 17 00:00:00 2001 From: Nicholas Bauer Date: Tue, 14 Oct 2025 12:50:40 -0400 Subject: [PATCH 4/4] Change to OutOfMemoryError --- base/docs/basedocs.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/base/docs/basedocs.jl b/base/docs/basedocs.jl index f9441b5be4368..b756de3c3e86e 100644 --- a/base/docs/basedocs.jl +++ b/base/docs/basedocs.jl @@ -1069,8 +1069,8 @@ try catch e if isa(e, EOFError) @warn "The operation failed - EOF." - elseif isa(e, IOError) - @warn "The operation failed - IO Error." + elseif isa(e, OutOfMemoryError) + @warn "The operation failed - OOM." else rethrow() # ensure other exceptions can bubble up the call stack end