I have console application which is executed in another app. The problem is that the second app cannot catch the exceptions that first one throws (run with Process). So I have to write some class that contains my return code and appropriate message.So that the app which runs my executable can use it and show error message depending on return code. But I don't know how this kind of thing should be done.
-
Why don't you start your `Process` within a `try-catch` and catch the error there? – Christoph K May 18 '16 at 11:25
-
`process.ExitCode` contains the return of `int main()` (or `Environment.Exit(X)`) – Alex K. May 18 '16 at 11:31
-
You can catch all exception in each application.Do you want it? – mohsen May 18 '16 at 11:39
-
ChristophKn: The first app cannot catch the exception that was thrown by second. – Gayane May 18 '16 at 11:41
-
Alex K: I understand but I also need error message , not only code. So the runner could somehow get message regarding to error code – Gayane May 18 '16 at 11:42
-
Use a dictionary
to map codes to strings? You could share an assembly hosting an enum of possible values. Or you could have the console app output the message string to stderr and read it back. – Alex K. May 18 '16 at 11:50
1 Answers
This is an old post i stumbled on but why not answer?
I think what you are looking for is the error stream. In windows and most other systems the OS references it as stderr
. I'll post some links for reading, but in essence, separate execution environments or applications only return codes that tell the OS weather or not an application exited (finished) successfully (0) or encountered an error (-1). In windows there are two separate streams stdout
which we write execution messages to, or stderr
which we write errors too. Those steams can then be read by your other application. Look at these for more info:
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx
Depending on your needs there are some other more complex solutions such as the "Interoperability Library" or writing to file.
Interop info: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interop/

- 1,885
- 3
- 27
- 50