{"record":{"id":"8d7393eda5623706","repo":"dotnet/wpf","slug":"throw-new-argumentexception-sr-format-sr","errorCode":null,"errorMessage":"throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));","messagePattern":"throw new ArgumentException\\(SR\\.Format\\(SR\\.GenericInvalidArgument, argName\\)\\);","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs","lineNumber":182,"sourceCode":"            ArgumentNullException.ThrowIfNull(e);\n\n            UiaCoreProviderApi.UiaRaiseStructureChangedEvent(provider, e.StructureChangeType, e.GetRuntimeId());\n        }\n        #endregion Public Methods\n\n\n        //------------------------------------------------------\n        //\n        //  Private Methods\n        //\n        //------------------------------------------------------\n\n        #region Private Methods\n\n        // Throw an argument Exception with a generic error\n        private static void ThrowInvalidArgument(string argName)\n        {\n            throw new ArgumentException(SR.Format(SR.GenericInvalidArgument, argName));\n        }\n\n        // Check that specified condition is true; if not, throw exception\n        private static void ValidateArgument(bool cond, string reason)\n        {\n            if (!cond)\n            {\n                throw new ArgumentException(SR.GetResourceString(reason, null));\n            }\n        }\n\n        #endregion Private Methods\n    }\n}\n","sourceCodeStart":164,"sourceCodeEnd":197,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationProvider/System/Windows/Automation/Provider/AutomationInteropProvider.cs#L164-L197","documentation":"AutomationInteropProvider.ThrowInvalidArgument throws a generic ArgumentException(SR.GenericInvalidArgument, argName) when RaiseAutomationEvent validation fails. It signals that one of the arguments passed to RaiseAutomationEvent (typically the event identifier or runtime element) is invalid per the private validation checks.","triggerScenarios":"Calling AutomationInteropProvider.RaiseAutomationEvent with an argument that fails ValidateArgument/ValidateCondition checks — e.g. a null or invalid AutomationEvent/runtimeId/element combination.","commonSituations":"WPF/WinForms custom control authors raising UIA events from provider code; passing an event object obtained from a different provider or a disposed element.","solutions":["Inspect the argName in the exception message to identify which argument is invalid","Verify the AutomationEvent was obtained via AutomationEvent.Register (or PatternEvent) and not constructed ad hoc","Ensure the source element is a valid, live AutomationPeer/RawElementProvider before raising the event","Add null checks for eventId, source, and args before calling RaiseAutomationEvent"],"exampleFix":"// before\nAutomationInteropProvider.RaiseAutomationEvent(myEvent, peer, args);\n// after\nif (myEvent == null || peer == null || args == null)\n    throw new ArgumentNullException(nameof(myEvent));\nAutomationInteropProvider.RaiseAutomationEvent(myEvent, peer, args);","handlingStrategy":"validation","validationCode":"if (eventId == null || source == null || args == null)\n    throw new ArgumentNullException(nameof(eventId));\n// eventId must come from AutomationEvent.Register / PatternEvent","typeGuard":"bool IsValidEvent(AutomationEvent e) => e != null && e.Id != 0;","tryCatchPattern":"try { AutomationInteropProvider.RaiseAutomationEvent(evt, peer, args); }\ncatch (ArgumentException ex) { log.Error($\"Invalid argument to RaiseAutomationEvent: {ex.ParamName}\"); }","preventionTips":["Only raise events obtained through AutomationEvent.Register or pattern event singletons","Null-check all arguments before calling provider APIs","Ensure the source AutomationPeer is live and attached to the tree"],"tags":["uiautomation","provider","argument","validation"],"backgroundTag":"invalid-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}