{"record":{"id":"b5cd529c86a30eb5","repo":"cefsharp/CefSharp","slug":"unexpected-failure-of-calling-cef-getzoomlevelasy","errorCode":null,"errorMessage":"Unexpected failure of calling CEF->GetZoomLevelAsync","messagePattern":"Unexpected failure of calling CEF->GetZoomLevelAsync","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"CefSharp.WinForms.Example/BrowserForm.cs","lineNumber":358,"sourceCode":"        }\n\n        private void ZoomInToolStripMenuItemClick(object sender, EventArgs e)\n        {\n            var control = GetCurrentTabControl();\n            if (control != null)\n            {\n                var task = control.Browser.GetZoomLevelAsync();\n\n                task.ContinueWith(previous =>\n                {\n                    if (previous.Status == TaskStatus.RanToCompletion)\n                    {\n                        var currentLevel = previous.Result;\n                        control.Browser.SetZoomLevel(currentLevel + ZoomIncrement);\n                    }\n                    else\n                    {\n                        throw new InvalidOperationException(\"Unexpected failure of calling CEF->GetZoomLevelAsync\", previous.Exception);\n                    }\n                }, TaskContinuationOptions.ExecuteSynchronously);\n            }\n        }\n\n        private void ZoomOutToolStripMenuItemClick(object sender, EventArgs e)\n        {\n            var control = GetCurrentTabControl();\n            if (control != null)\n            {\n                var task = control.Browser.GetZoomLevelAsync();\n                task.ContinueWith(previous =>\n                {\n                    if (previous.Status == TaskStatus.RanToCompletion)\n                    {\n                        var currentLevel = previous.Result;\n                        control.Browser.SetZoomLevel(currentLevel - ZoomIncrement);\n                    }","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.WinForms.Example/BrowserForm.cs#L340-L376","documentation":"Thrown inside the ZoomIn menu handler's task continuation when GetZoomLevelAsync did not complete successfully (faulted or canceled). The continuation checks task status and throws InvalidOperationException wrapping the original AggregateException so the failure is surfaced rather than silently swallowed. With TaskContinuationOptions.ExecuteSynchronously this throws on the captured context.","triggerScenarios":"GetZoomLevelAsync faults — typically because the browser/frame was disposed or not yet initialized, or the CEF IPC call to retrieve zoom failed. Can also fire if the underlying CEF host is invalid.","commonSituations":"Clicking Zoom In while the browser is mid-navigation, during shutdown, or before initialization completes. The exception is thrown in a fire-and-forget continuation so it may surface as an unobserved task exception.","solutions":["Gate the zoom handler on IsBrowserInitialized and not IsDisposed.","Observe the task via await instead of ContinueWith+throw to handle faults cleanly.","Log previous.Exception instead of rethrowing to avoid unobserved-exception crashes."],"exampleFix":"// before\ntask.ContinueWith(p => {\n  if (p.Status != TaskStatus.RanToCompletion)\n    throw new InvalidOperationException(\"...\", p.Exception);\n}, TaskContinuationOptions.ExecuteSynchronously);\n\n// after\ntry { var level = await control.Browser.GetZoomLevelAsync();\n       control.Browser.SetZoomLevel(level + ZoomIncrement); }\ncatch (Exception ex) { /* log */ }","handlingStrategy":"try-catch","validationCode":"if (control?.Browser == null || !control.Browser.IsBrowserInitialized || control.Browser.IsDisposed) return;","typeGuard":"public static bool CanQueryZoom(IWebBrowser b) => b != null && b.IsBrowserInitialized && !b.IsDisposed;","tryCatchPattern":"try { var level = await control.Browser.GetZoomLevelAsync();\n      control.Browser.SetZoomLevel(level + ZoomIncrement); }\ncatch (InvalidOperationException) { /* zoom query failed, skip */ }","preventionTips":["Gate zoom handlers on IsBrowserInitialized and not IsDisposed.","Prefer await over ContinueWith+throw to observe faults.","Do not rethrow in fire-and-forget continuations — log instead."],"tags":["winforms","zoom","task-continuation","example-code","unobserved-exception"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}