{"record":{"id":"864a092a711b452c","repo":"dotnet/AspNetCore.Docs","slug":"current-count-is-too-big","errorCode":null,"errorMessage":"Current count is too big!","messagePattern":"Current count is too big!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"aspnetcore/blazor/fundamentals/handle-errors.md","lineNumber":285,"sourceCode":"\n`EmbeddedCounter.razor`:\n\n```razor\n<h1>Embedded Counter</h1>\n\n<p role=\"status\">Current count: @currentCount</p>\n<button class=\"btn btn-primary\" @onclick=\"IncrementCount\">Click me</button>\n\n@code {\n    private int currentCount = 0;\n\n    private void IncrementCount()\n    {\n        currentCount++;\n\n        if (currentCount > 5)\n        {\n            throw new InvalidOperationException(\"Current count is too big!\");\n        }\n    }\n}\n```\n\n`Home.razor`:\n\n```razor\n@page \"/\"\n@rendermode InteractiveServer\n\n<PageTitle>Home</PageTitle>\n\n<h1>Home</h1>\n\n<ErrorBoundary>\n    <EmbeddedCounter />\n</ErrorBoundary>","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/blazor/fundamentals/handle-errors.md#L267-L303","documentation":"This is intentionally-thrown demo code. The Counter component throws InvalidOperationException(\"Current count is too big!\") once currentCount exceeds 5. The sample exists to demonstrate Blazor's default unhandled-exception behavior (the developer-exception page in Development, or the error UI in Production) for interactive server components. It is not a library error; it is a teaching throw.","triggerScenarios":"Clicking the 'Click me' button more than 5 times in a row in the sample Counter/Home component. Each IncrementCount increments currentCount; on the 6th click the condition (currentCount > 5) becomes true and the exception is thrown.","commonSituations":"Running the docs sample locally and clicking past 5; copying the sample into your own app to learn Blazor error boundaries. Not a production condition — the message is illustrative.","solutions":["Recognize this is sample code: to stop the throw, either remove the if-block or raise the threshold.","If learning error handling, add an <ErrorBoundary> around the component to catch the rendered exception gracefully.","If you copied this into real code by mistake, replace the artificial check with real domain validation.","For production, never throw on a benign counter overflow — return early or cap the value instead."],"exampleFix":"// before\nif (currentCount > 5)\n{\n    throw new InvalidOperationException(\"Current count is too big!\");\n}\n\n// after — cap instead of throwing\nif (currentCount > 5)\n{\n    currentCount = 5;\n    return;\n}","handlingStrategy":"validation","validationCode":"// Prevent the demo throw by capping before increment\nif (currentCount >= 5) { return; }\ncurrentCount++;","typeGuard":null,"tryCatchPattern":"<ErrorBoundary> @* wrap component so throw is caught *@\n    <ChildComponent />\n    <ChildContent>...</ChildContent>\n</ErrorBoundary>","preventionTips":["Do not ship demo thresholds in production code.","Wrap interactive components in <ErrorBoundary>.","Replace artificial throws with real domain validation.","Cap input-driven values rather than throwing."],"tags":["blazor","demo","invalidoperationexception","error-boundary","server"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}