Does anybody know how to deal with the System.Collection.Genetic.KeyNotFoundException?

Here is the thing, I wrote a program for visual graph analysis using C# script. The program is not very complicated, but sometimes I got this System.Collection.Genetic.KeyNotFoundException error, not in every condition. (14.5 KB) think my program is correct. Can anybody help me out?

Moved to Grasshopper category.