user3537759
user3537759

Reputation: 11

Is there a way to capture error in one application in other application

I am working on two different applications.

I am calling Application 2 process (.exe file ) in Application 1 Solution. When Application 2 throws an “Invalid Username and Password” error I want to catch that error exception in Application 1 solution.

Is there a way to capture error in one application in other application

My Application is a C# windows application

Upvotes: 1

Views: 512

Answers (3)

armstrom
armstrom

Reputation: 59

Kind of... You can do it if you don't actually launch another process but rather create a separate AppDomain within your existing process to run your executable in. The "inner" executable must also be a CLR assembly. The other answers indicating that cross-process exception handling isn't possible are correct. This approach just works around the problem while hopefully giving you the behavior you're after.

Just starting the process normally won't get you what you want.

var process = Process.Start("MyInnerProcess.exe");

You will get back a Process object that gives you all sorts of juicy information about the process and allows you to monitor things being written to the standard output streams... but no real access to the exceptions thrown within the process.

However, if you first haul-up a new AppDomain to launch your assembly into you can quite easily receive notification of all exceptions (both unhandled and first chance!) thrown by that assembly while running. (Note: There's nothing you can DO about these exceptions... but you will know they are happening.)

Here's the code:

var innerDomain = AppDomain.CreateDomain("InnerDomain");
try
{
    //Subscribe to the events you are interested in     
    innerDomain.UnhandledException += innerDomain_UnhandledException;
    innerDomain.FirstChanceException += innerDomain_FirstChanceException;

    //Execute your assembly within the app domain
    innerDomain.ExecuteAssembly("MyInnerProcess.exe");
}
catch (Exception ex)
{
    //Handle exceptions when attempting to launch the process itself.
}

void innerDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    //Do something with the unhandled exceptions
}
void innerDomain_FirstChanceException(object sender, FirstChanceExceptionEventArgs e)
{
    //Do something with the first chance exceptions
}

Now, whenever an exception is thrown in the other process you will get access to that exception. The FirstChanceException handler will be called for EVERY exception (even if it's handled properly inside the application) so you may not want to subscribe to it. You're probably most interested in the UnhandledException event. Also keep in mind that all unhandled exceptions will first fire the FirstChanceException (when they are thrown) then fire the UnhandledException if they manage to bubble all the way up without being handled.

Upvotes: 2

Barry West
Barry West

Reputation: 552

Adding on to Jordan's Solution, you can read the console out stream of Application2.

Ref: http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput(v=vs.110).aspx

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

Upvotes: 0

Jordan Parmer
Jordan Parmer

Reputation: 37234

You cannot throw-catch between process boundaries. You have to use an inter-process communication technique. There are many of them to choose from depending on your situation.

Here is a list of some options...

  1. File: Process A writes to a log file that process B is listening to. Very easy to implement.
  2. Named pipes or TCP/IP sockets: You can link two processes together with a named pipe or sockets and transmit data over the wire from one application to another.
  3. Message queue: Process A can listen to a message queue that process B is pushing messages to.
  4. Database: Process A can write to a database while process B checks the database. This could be a remote database or something as simple as a SQLite database.

Using a file or named pipe is likely the easiest way to go about what you are trying to solve. The other options would depend on your environment and requirements.

Upvotes: 1

Related Questions