Search code examples
c#error-handlingunhandled-exceptionlaunching-application

catch another process unhandled exception


I wish to know if i can catch the unhandled exceptions thrown by another process which I started using the Process.Start(...)

I know i can catch the standered error using this link , but what I want is to catch the error that are usually caught by the Just In Time debugger of the.net environment, the window with the following words: "An unhandled exception has occurred in your application . If you Continue, the application will ignore this error and attempt to continue . If you click Quit, the application will be shut down immediately ...." Which is then followed by the exception message and a "Continue" and "Quit" button.


Solution

  • If you are calling to a .Net executable assembly you can load it and (at your own risk :D ) call to the Main method of the Program class into a try_catch statement:

    Assembly assembly = Assembly.LoadFrom("ErroneusApp.exe");
    Type[] types= assembly.GetTypes();
    foreach (Type t in types)
    {
     MethodInfo method = t.GetMethod("Main",
         BindingFlags.Static | BindingFlags.NonPublic);
     if (method != null)
     {
        try
        {
            method.Invoke(null, null);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        break;
     }
    }
    

    But be aware of the security risks you are introducing doing that.