rethrow
rethrow([e])
Throw an object without changing the current exception backtrace. The default argument is the current exception (if called within a catch block).
Examples
-
Rethrow the current exception:
try # Some code that might throw an exception catch ex # Handle the exception rethrow() endThis example demonstrates the use of
rethrow()within acatchblock to rethrow the current exception without modifying the backtrace. -
Rethrow a specific exception:
try # Some code that might throw an exception catch ex if isa(ex, MyCustomException) # Handle specific exception else # Rethrow the exception rethrow(ex) end endIn this example,
rethrow(ex)is used to rethrow a specific exception that meets certain conditions, while handling other exceptions differently. - Rethrow with a modified backtrace:
try # Some code that might throw an exception catch ex # Modify the exception or backtrace modified_ex = modify_exception(ex) modified_ex.backtrace = ... rethrow(modified_ex) endThis example demonstrates how
rethrow()can be used to rethrow an exception with a modified backtrace or other modified attributes.
Common mistake example:
try
# Some code that might throw an exception
catch ex
# Rethrow the exception with a new message
rethrow("Custom message")
end
In this example, rethrow() is mistakenly used to rethrow an exception with a new message. However, rethrow() does not accept a new message as an argument. To modify the exception message, it is better to create a new exception and throw it using throw().
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.