How do I keep the WebBrowser control from catching exceptions and displaying them as script errors?-Collection of common programming errors
I didn’t end up with a universal solution, but this one works well enough for me.
Communication between the browser and my application is only done through a single Javascript method. So I just wrapped all of the code in that method in a try/catch block and threw the exception on another thread.
[ComVisible(true)]
public class JavascriptBridge {
public void Notify(string data) {
try {
// Process the data
} catch (Exception ex) {
System.Threading.ThreadPool.QueueUserWorkItem((state) => {
throw new Exception("An unhandled exception ocurred while servicing a Javascript method call.", ex);
});
}
}
}
This completely bypasses the browser control’s exception handler and retains the original exception’s location information. Note, however, that the only way to catch the exception again is with the Application.ThreadException
event.