{"record":{"id":"1d89a7011786e7de","repo":"dotnet/AspNetCore.Docs","slug":"current-count-is-over-five","errorCode":null,"errorMessage":"Current count is over five!","messagePattern":"Current count is over five!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"aspnetcore/blazor/fundamentals/handle-errors.md","lineNumber":618,"sourceCode":"\n* Call an error processing method in any `catch` block with an appropriate exception type. The example `ProcessError` component only offers a single `LogError` method, but the error processing component can provide any number of error processing methods to address alternative error processing requirements throughout the app. The following `Counter` component `@code` block example includes the `ProcessError` cascading parameter and traps an exception for logging when the count is greater than five:\n\n  ```razor\n  @code {\n      private int currentCount = 0;\n\n      [CascadingParameter]\n      private ProcessError? ProcessError { get; set; }\n\n      private void IncrementCount()\n      {\n          try\n          {\n              currentCount++;\n\n              if (currentCount > 5)\n              {\n                  throw new InvalidOperationException(\"Current count is over five!\");\n              }\n          }\n          catch (Exception ex)\n          {\n              ProcessError?.LogError(ex);\n          }\n      }\n  }\n  ```\n\nThe logged error:\n\n> :::no-loc text=\"fail: {COMPONENT NAMESPACE}.ProcessError[0]\":::  \n> :::no-loc text=\"ProcessError.LogError: System.InvalidOperationException Message: Current count is over five!\":::\n\nIf the `LogError` method directly participates in rendering, such as showing a custom error message bar or changing the CSS styles of the rendered elements, call [`StateHasChanged`](xref:blazor/components/lifecycle#state-changes-statehaschanged) at the end of the `LogError` method to rerender the UI.\n\nBecause the approaches in this section handle errors with a [`try-catch`](/dotnet/csharp/language-reference/keywords/try-catch) statement, an app's SignalR connection between the client and server isn't broken when an error occurs and the circuit remains alive. Other unhandled exceptions remain fatal to a circuit. For more information, see the section on [how a circuit reacts to unhandled exceptions](#unhandled-exceptions-for-circuits).","sourceCodeStart":600,"sourceCodeEnd":636,"githubUrl":"https://github.com/dotnet/AspNetCore.Docs/blob/c67a80103a1a74db20784debd919c7fdda96c510/aspnetcore/blazor/fundamentals/handle-errors.md#L600-L636","documentation":"Demo throw used to show Blazor's global error processing via a [CascadingParameter] ProcessError component. When currentCount exceeds 5 the component throws InvalidOperationException(\"Current count is over five!\"), but unlike errors 42/43 it is caught locally and forwarded to ProcessError?.LogError(ex) for centralized logging/UI. Illustrates graceful per-component error capture.","triggerScenarios":"Incrementing the counter past 5. The local try/catch swallows the exception and routes it to the cascaded ProcessError service, which logs it.","commonSituations":"Adopting the documented global error-handling pattern; wiring a ProcessError cascading value across components; testing that LogError fires on the rendered exception.","solutions":["Ensure a ProcessError component is supplied as a CascadingValue ancestor so ProcessError is non-null (otherwise errors are silently swallowed).","Verify ProcessError.LogError actually persists/logs (check the logged output shown in the doc).","Replace the artificial threshold with real domain logic in production.","Consider rethrowing or surfacing UI feedback after logging if the user must be notified."],"exampleFix":"// before\ncatch (Exception ex)\n{\n    ProcessError?.LogError(ex);\n}\n\n// after — also notify the user and guard against null cascading value\ncatch (Exception ex)\n{\n    if (ProcessError is null)\n    {\n        logger.LogError(ex, \"No ProcessError cascading value available.\");\n    }\n    else\n    {\n        ProcessError.LogError(ex);\n    }\n    errorMessage = \"Something went wrong. Please try again.\";\n}","handlingStrategy":"try-catch","validationCode":"if (currentCount > 5) { currentCount = 5; return; }","typeGuard":null,"tryCatchPattern":"try { /* increment logic */ }\ncatch (Exception ex)\n{\n    if (ProcessError is not null) ProcessError.LogError(ex);\n    else logger.LogError(ex, \"ProcessError cascading value not supplied.\");\n    errorMessage = \"Unable to increment.\";\n}","preventionTips":["Always supply a ProcessError CascadingValue ancestor so the cascading parameter is non-null.","Verify ProcessError.LogError actually persists.","Surface user feedback after logging.","Replace artificial thresholds with real validation."],"tags":["blazor","demo","invalidoperationexception","error-handling","cascading-parameter"],"backgroundTag":null,"analyzedSha":"c67a80103a1a74db20784debd919c7fdda96c510","analyzedAt":"2026-08-13T17:46:11.763Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}