error
error(message::AbstractString)
Raise an ErrorException with the given message
Examples
julia> error("Something went wrong.")
ERROR: Something went wrong.
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] top-level scope at REPL[1]:1
Common examples:
-
Throw a custom error message:
julia> error("Invalid input.") ERROR: Invalid input. Stacktrace: [1] error(::String) at ./error.jl:33This example raises an
ErrorExceptionwith the message "Invalid input.". -
Use error in a function:
function divide(a, b) if b == 0 error("Cannot divide by zero.") else return a / b end end julia> divide(10, 0) ERROR: Cannot divide by zero. Stacktrace: [1] error(::String) at ./error.jl:33In this example, the
dividefunction throws an error if the divisor (b) is zero. -
Handle error with a try-catch block:
try error("This is an error.") catch e println("Caught an error:", e) endThis example demonstrates how to catch and handle the
ErrorException. The error message is printed if an error occurs.
Remember to provide a specific error message that describes the issue to aid debugging.
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.