{"record":{"id":"17e746a705451fa9","repo":"microsoft/semantic-kernel","slug":"invalid-choice","errorCode":null,"errorMessage":"Invalid choice","messagePattern":"Invalid choice","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/AmazonBedrockModels/Program.cs","lineNumber":34,"sourceCode":"// Get user choice\nint choice = GetUserChoice();\n\nswitch (choice)\n{\n    case 1:\n        await PerformChatCompletion().ConfigureAwait(false);\n        break;\n    case 2:\n        await PerformTextGeneration().ConfigureAwait(false);\n        break;\n    case 3:\n        await PerformStreamChatCompletion().ConfigureAwait(false);\n        break;\n    case 4:\n        await PerformStreamTextGeneration().ConfigureAwait(false);\n        break;\n    default:\n        throw new InvalidOperationException(\"Invalid choice\");\n}\n\nasync Task PerformChatCompletion()\n{\n    string userInput;\n    ChatHistory chatHistory = [];\n\n    // Get available chat completion models\n    var availableChatModels = bedrockModels.Values\n        .Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.ChatCompletion))\n        .ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name);\n\n    // Show user what models are available and let them choose\n    int chosenModel = GetModelNumber(availableChatModels, \"chat completion\");\n\n    var kernel = Kernel.CreateBuilder().AddBedrockChatCompletionService(availableChatModels[chosenModel]).Build();\n    var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/AmazonBedrockModels/Program.cs#L16-L52","documentation":"Thrown by the default arm of a console-menu switch in the Amazon Bedrock Models sample. Cases 1-4 dispatch to chat/text/stream demos; any other value falls through to a hard InvalidOperationException. It is a programmer/user-input guard, not a library error, so the message 'Invalid choice' refers to the menu digit entered at runtime.","triggerScenarios":"The user types a number outside 1-4 (e.g. 0, 5, 9) or a non-integer string that was parsed to a value outside the supported range and reached the `default` branch of the switch at Program.cs:34.","commonSituations":"Running the interactive demo and mistyping the menu option, an automated script feeding an out-of-range index, or the menu being extended with new cases without updating valid input bounds.","solutions":["Re-run the sample and enter a digit strictly between 1 and 4.","If scripting the demo, clamp/validate the parsed integer before the switch and re-prompt on invalid input.","If you extended the menu, add the new case branch before the default so valid new options do not throw."],"exampleFix":"// before\ndefault:\n    throw new InvalidOperationException(\"Invalid choice\");\n\n// after\nint choice;\nwhile (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)\n{\n    Console.Write(\"Enter a number from 1 to 4: \");\n}\nswitch (choice) { /* cases 1-4, no default throw */ }","handlingStrategy":"validation","validationCode":"int choice;\nwhile (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)\n{\n    Console.Write(\"Enter a number from 1 to 4: \");\n}\n// choice is now guaranteed valid; the default throw is unreachable","typeGuard":"static bool IsValidMenuChoice(string input, out int value) =>\n    int.TryParse(input, out value) && value is >= 1 and <= 4;","tryCatchPattern":null,"preventionTips":["Bound-check parsed menu input before the switch.","Re-prompt on invalid input instead of throwing.","Add new switch arms whenever you extend AllowedValues."],"tags":["user-input","menu","validation","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}