From Xojo Documentation

Revision as of 16:48, 5 January 2022 by Gperlman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!

Class (inherits from RuntimeException)

You tried to access an item in a Dictionary using a key that is not in the Dictionary.

ErrorNumber Message
Stack StackFrames


You can avoid this exception by making use of the Dictionary.Lookup method to provide a default value if the key is not found or by using the Dictionary.HasKey method to check if the key exists.

Sample Code

The following For...Next loop produces a KeyNotFoundException error when the loop runs with the counter equal to 1. The value of 1 — which is a Variant, not an Integer — has just been removed.

Var d As New Dictionary
Var i As Integer
d.Value(0) = "Clark Kent"
d.Value(1) = "Lois Lane"
d.Value(2) = "Jimmy Olsen"
For i = 0 To d.Count - 1 // fails when i=1

To handle the error, add an Exception block to the end of the method, such as:

Exception err As KeyNotFoundException
MsgBox("You tried to access a nonexistent item!")

See Also

Dictionary, RuntimeException classes; Exception block.