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:

  1. 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 ArgumentError when the greet function is called without any arguments.

  2. 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 ArgumentError is thrown when the calculate_square function is called with a non-numeric argument.

  3. 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 ArgumentError when the divide function 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.

*Required Field
Details

Checking you are not a robot: