showerror
showerror(io, e)
Show a descriptive representation of an exception object.
Examples
In the Julia programming language, the function showerror(io, e)
is used to display a descriptive representation of an exception object.
-
Display exception information:
julia> try error("Oops! Something went wrong.") catch err showerror(stdout, err) end ERROR: Oops! Something went wrong. Stacktrace: [1] error(s::String) @ Base ./error.jl:33 [2] top-level scope @ REPL[1]:2
This example shows how to use
showerror
to display the error message and stacktrace of an exception object. Thestdout
argument specifies that the output should be printed to the standard output. -
Capture exception information to a string:
julia> try error("Oops! Something went wrong.") catch err str = IOBuffer() showerror(str, err) message = String(take!(str)) end "ERROR: Oops! Something went wrong.\nStacktrace:\n [1] error(s::String)\n @ Base ./error.jl:33\n [2] top-level scope\n @ REPL[1]:2\n"
This example demonstrates how to capture the exception information to a string. By using an
IOBuffer
, the output ofshowerror
is stored in the buffer, which can then be converted to a string.
Common mistake example:
julia> try
error("Oops! Something went wrong.")
catch err
showerror(err)
end
ERROR: MethodError: no method matching showerror(::ErrorException)
Closest candidates are:
showerror(::IO, ::Any) at errorshow.jl:47
Stacktrace:
[1] top-level scope
@ REPL[1]:5
In this example, showerror
is called without specifying the IO
argument. It is essential to provide an IO
object as the first argument to showerror
to indicate where the error message should be displayed.
See Also
ArgumentError, AssertionError, BoundsError, DivideError, DomainError, EOFError, error, ErrorException, InexactError, InitError, KeyError, LoadError, MethodError, OutOfMemoryError, OverflowError, ParseError, ReadOnlyMemoryError, showerror, StackOverflowError, SystemError, TypeError, UndefRefError, UndefVarError,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.