1

I have an issue with an external component that throws System.AccessViolationException. The problem is that this component is a plugin of Internet Explorer/Edge used in System.Windows.Forms.WebBrowser. So navigating to a special webpage triggers the web browser to load an ActiveX plugin which sometimes fails. In the case of this exception, my whole application is terminated. I am able to catch the exception by enabling legacyCorruptedStateExceptionsPolicy in config and setting up

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;

in my Program.cs, but the IsTerminating flag is set and my app will crash afterward. Since I have several windows opened using Form.Show and one of it is hosting the WebBrowser control: is there a way to catch this exception within my Form controlling the web browser and just terminate this one? So the rest of the application is still alive? Or even better: is it possible to handle the exception, navigate to a blank page displaying just a short error message?

Thank you very much for your help!

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
Fabi
  • 11
  • 3
  • The best approach is to farm this window out to a separate process entirely and communicate with it through IPC (if necessary) so it crashing doesn't take down your process. The runtime intentionally makes it hard to impossible to prevent these exceptions from terminating the process, as they are usually a sign of state corruption that the process shouldn't even try to recover from (whether that happens to be true or not). There are various legacy flags/attributes you can still try to control the behavior, but it's only going to get more complicated as time goes on. – Jeroen Mostert Dec 22 '20 at 11:29
  • Does this answer your question? [Is it possible to catch an access violation exception in .NET?](https://stackoverflow.com/questions/3312003/is-it-possible-to-catch-an-access-violation-exception-in-net) – Jeroen Mostert Dec 22 '20 at 11:30
  • yes all the threads are very similiar to my issue but I hoped there is a way not creating a single process. Since issue is raised within WebBrowser I am not able to do something like this: `[HandleProcessCorruptedStateExceptions] public void MyMethod() { try { NaughtyCall(); } catch (AccessViolationException e) { // You should really terminate your application here } }` So I have to refactor the application and start my Form from Program.cs using `Application.Run()` and add pipes between these processes? – Fabi Dec 22 '20 at 12:12

0 Answers0