Prevent WebBrowser control from swallowing exceptions-Collection of common programming errors
I’m using the System.Windows.Forms.WebBrowser
, to make a view a-la Visual Studio Start Page. However, it seems the control is catching and handling all exceptions by silently sinking them! No need to tell this is a very unfortunate behaviour.
void webBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
// WebBrowser.Navigating event handler
throw new Exception("OMG!");
}
The code above will cancel navigation and swallow the exception.
void webBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
// WebBrowser.Navigating event handler
try
{
e.Cancel = true;
if (actions.ContainsKey(e.Url.ToString()))
{
actions[e.Url.ToString()].Invoke(e.Url, webBrowser.Document);
}
}
catch (Exception exception)
{
MessageBox.Show(exception.ToString());
}
}
So, what I do (above) is catch all exceptions and pop a box, this is better than silently failing but still clearly far from ideal. I’d like it to redirect the exception through the normal application failure path so that it ultimately becomes unhandled, or handled by the application from the root.
Is there any way to tell the WebBrowser
control to stop sinking the exceptions and just forward them the natural and expected way? Or is there some hacky way to throw an exception through native boundaries?
-
My best bet why it happens is because there is a native-managed-native boundary to cross. The native part doesn’t forward the managed exceptions correctly and there is not much that can be done.
I am still hoping for a better answer though.
-
I haven’t seen the browser eat exceptions, unless you mean script errors. Script errors can be enabled via the
browser.ScriptErrorsSuppressed
property.If you’re talking about real exceptions, not just script errors, can you show us some code that reproduces the problem? We’ve used the browser extensively and haven’t seen what you’re describing.
edit the code sample wasn’t there when I asked for a code sample