ArgumentError
ArgumentError(msg)
The parameters to a function call do not match a valid signature.
Argument msg is a descriptive error string.
Examples
In the Julia programming language, the function ArgumentError(msg) is used to raise an exception when the parameters provided to a function call do not match a valid signature.
julia> ArgumentError("Invalid number of arguments.")
ERROR: ArgumentError: Invalid number of arguments.
Here are some common examples of how ArgumentError can be used:
-
Invalid number of arguments:
julia> function greet(name) if name == "" throw(ArgumentError("Name cannot be empty.")) else println("Hello, $name!") end end julia> greet() ERROR: ArgumentError: Invalid number of arguments.This example throws an
ArgumentErrorwhen thegreetfunction is called without any arguments. -
Invalid argument type:
julia> function calculate_square(n) if !isa(n, Number) throw(ArgumentError("Invalid argument type. Expected a number.")) else println(n * n) end end julia> calculate_square("5") ERROR: ArgumentError: Invalid argument type. Expected a number.In this example, an
ArgumentErroris thrown when thecalculate_squarefunction is called with a non-numeric argument. -
Missing required argument:
julia> function divide(a, b) if b == 0 throw(ArgumentError("Cannot divide by zero.")) else println(a / b) end end julia> divide(10) ERROR: ArgumentError: Invalid number of arguments.This example raises an
ArgumentErrorwhen thedividefunction is called without providing the required second argument.
Common mistake example:
julia> ArgumentError("Invalid number of arguments.")
ERROR: ArgumentError: Invalid number of arguments.
The common mistake with ArgumentError is to forget to use the throw keyword when raising the exception. Simply creating an ArgumentError object without throwing it will not have the desired effect.
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.