Reputation: 11
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
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
Reputation: 552
Adding on to Jordan's Solution, you can read the console out stream of Application2.
// 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
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...
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