1
我正在製作一個程序,將具有特定密鑰的不同參與者放入庫中。我來到了我得到KeyNotFoundException
的問題。KeyNotFoundException未處理,將密鑰放入庫
在這裏你可以看到我的代碼錯誤這是在if (sessionPersonsSchedule[s].Count == s.MaxParticipants)
public bool personAddToLibrary(int personsIn)
{
if (personsIn == participants.Count)
{
return true;
}
else
{
for (int j = 0; j < participants[personsIn].Preferences.Count; j++)
{
Participant p = participants[personsIn];
Session s = sessions[p.Preferences[j] - 1];
SessionPersonsSchedule[s]
if (testSessions(s))
{
sessionPersonsSchedule[s].Add(p);
Console.Write(personsIn + ". " + p + "is added to session " + s);
personAddToLibrary(personsIn + 1);
if (personsIn == participants.Count)
{
return true;
}
}
else
{
sessionPersonsSchedule[s].Remove(p);
Console.Write(personsIn + ". " + p + "is deleted from session " + s);
}
}
}
return false;
}
public bool testSessions(Session s)
{
if (sessionPersonsSchedule[s].Count == s.MaxParticipants)
{
return false;
}
else
{
return true;
}
}
請參閱:http://stackoverflow.com/questions/606636/best-way-to-handle-a-keynotfoundexception – Vova