ErrorException

ErrorException(msg)

Generic error type. The error message, in the .msg field, may provide more specific details.

Examples

  1. Create a generic error with a custom message:

    julia> err = ErrorException("Something went wrong.")
    ErrorException("Something went wrong.")

    This example creates a generic error using the ErrorException constructor and assigns it to the variable err. The error message is set to "Something went wrong."

  2. Access the error message from the .msg field:

    julia> err = ErrorException("Division by zero.")
    ErrorException("Division by zero.")
    
    julia> err.msg
    "Division by zero."

    Here, we create an error with the message "Division by zero." Then, we access the error message using the .msg field of the ErrorException object.

  3. Throw a custom error using ErrorException:

    julia> function divide(a, b)
              if b == 0
                  error(ErrorException("Division by zero is not allowed."))
              else
                  return a / b
              end
          end
    
    julia> divide(10, 0)
    ERROR: ErrorException("Division by zero is not allowed.")
    Stacktrace:
    [1] divide(a::Int64, b::Int64)
      @ Main REPL[1]:3
    [2] top-level scope
      @ REPL[2]:1

    In this example, we define a function divide that throws a custom error using ErrorException if the second argument b is zero. When calling divide(10, 0), it throws an error with the message "Division by zero is not allowed."

Common mistake example:

julia> err = ErrorException(123)
ERROR: MethodError: Cannot `convert` an object of type Int64 to an object of type String

In this example, the error message is provided as an integer (123) instead of a string. The ErrorException expects the error message to be a string, so it results in a MethodError. Always ensure that the error message passed to ErrorException is a string to avoid this mistake.

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: