{"record":{"id":"51b3e3a64f862ed6","repo":"lepoco/wpfui","slug":"window-handle-cannot-be-empty","errorCode":null,"errorMessage":"Window handle cannot be empty","messagePattern":"Window handle cannot be empty","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/Wpf.Ui/Appearance/SystemThemeWatcher.cs","lineNumber":77,"sourceCode":"        {\n            System.Diagnostics.Debug.WriteLine(\n                $\"INFO | {typeof(SystemThemeWatcher)} changed the app theme on initialization.\",\n                nameof(SystemThemeWatcher)\n            );\n            ApplicationThemeManager.ApplySystemTheme(updateAccents);\n        }\n    }\n\n    private static void ObserveLoadedWindow(Window window, WindowBackdropType backdrop, bool updateAccents)\n    {\n        IntPtr hWnd =\n            (hWnd = new WindowInteropHelper(window).Handle) == IntPtr.Zero\n                ? throw new InvalidOperationException(\"Could not get window handle.\")\n                : hWnd;\n\n        if (hWnd == IntPtr.Zero)\n        {\n            throw new InvalidOperationException(\"Window handle cannot be empty\");\n        }\n\n        ObserveLoadedHandle(new ObservedWindow(hWnd, backdrop, updateAccents));\n    }\n\n    private static void ObserveWindowWhenLoaded(\n        Window window,\n        WindowBackdropType backdrop,\n        bool updateAccents\n    )\n    {\n        window.Loaded += (_, _) =>\n        {\n            IntPtr hWnd =\n                (hWnd = new WindowInteropHelper(window).Handle) == IntPtr.Zero\n                    ? throw new InvalidOperationException(\"Could not get window handle.\")\n                    : hWnd;\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Appearance/SystemThemeWatcher.cs#L59-L95","documentation":"This is the 'Window handle cannot be empty' guard inside ObserveLoadedWindow. It is effectively dead code: the preceding ternary already throws InvalidOperationException when hWnd == IntPtr.Zero, so this if-block can never execute. It documents intent but cannot be the actual cause of a thrown exception in that method.","triggerScenarios":"Unreachable in normal execution because the assignment-ternary throws first whenever the handle is zero. It would only be the active throw site if the ternary expression were removed or refactored.","commonSituations":"Developers reading the stack trace will not see this line as the throw origin; the real throw is at the ternary (error 5). The duplicate check is a maintenance/complexity smell.","solutions":["Treat error 5 (the ternary throw) as the real source of a zero-handle failure in ObserveLoadedWindow; resolve the underlying too-early-watch issue there.","Optionally remove the redundant if-block to reduce confusion, since it can never fire.","Add an XML doc comment clarifying that the ternary is the authoritative guard."],"exampleFix":"// before\nIntPtr hWnd =\n    (hWnd = new WindowInteropHelper(window).Handle) == IntPtr.Zero\n        ? throw new InvalidOperationException(\"Could not get window handle.\")\n        : hWnd;\nif (hWnd == IntPtr.Zero)\n{\n    throw new InvalidOperationException(\"Window handle cannot be empty\");\n}\n\n// after\nIntPtr hWnd =\n    (hWnd = new WindowInteropHelper(window).Handle) == IntPtr.Zero\n        ? throw new InvalidOperationException(\"Could not get window handle.\")\n        : hWnd;","handlingStrategy":"validation","validationCode":"// No runtime guard needed; this block is unreachable. Remove it or rely on the ternary guard above.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Recognize this as dead code; the real throw is the ternary in error 5.","Keep a single authoritative guard per code path to avoid confusion.","Review duplicated checks during code review for reachability."],"tags":["dead-code","hwnd","maintenance"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}