ErrorException
ErrorException(msg)
Generic error type. The error message, in the .msg field, may provide more specific details.
Examples
-
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
ErrorExceptionconstructor and assigns it to the variableerr. The error message is set to "Something went wrong." -
Access the error message from the
.msgfield: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
.msgfield of theErrorExceptionobject. -
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]:1In this example, we define a function
dividethat throws a custom error usingErrorExceptionif the second argumentbis zero. When callingdivide(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.