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:

  1. Throw a custom error message:

    julia> error("Invalid input.")
    ERROR: Invalid input.
    Stacktrace:
    [1] error(::String) at ./error.jl:33

    This example raises an ErrorException with the message "Invalid input.".

  2. 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:33

    In this example, the divide function throws an error if the divisor (b) is zero.

  3. Handle error with a try-catch block:

    try
       error("This is an error.")
    catch e
       println("Caught an error:", e)
    end

    This 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.

*Required Field
Details

Checking you are not a robot: