KeyError

KeyError(key)

An indexing operation into an Associative (Dict) or Set like object tried to access or delete a non-existent element.

Examples

In the Julia programming language, the function KeyError(key)

This function is typically used when an indexing operation into an Associative (Dict) or Set-like object attempts to access or delete a non-existent element.

julia> d = Dict("apple" => 1, "banana" => 2, "orange" => 3);

julia> d["grape"]
ERROR: KeyError: key "grape" not found

Common example of its use:

julia> d = Dict("apple" => 1, "banana" => 2, "orange" => 3);

julia> key = "mango";

julia> try
           d[key]
       catch err
           if isa(err, KeyError)
               println("KeyError occurred for key: $key")
           else
               rethrow(err)
           end
       end
KeyError occurred for key: mango

This example demonstrates the use of KeyError in handling an exception when accessing a non-existent key in a dictionary (Dict). It catches the KeyError and performs custom error handling.

It's important to note that KeyError can also be raised when attempting to delete a non-existent key from an Associative (Dict) or Set-like object.

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: