How to catch exceptions from processes in C #

I have a receiver program that looks something like this:

public Result Run(CommandParser parser) { var result = new Result(); var watch = new Stopwatch(); watch.Start(); try { _testConsole.Start(); parser.ForEachInput(input => { _testConsole.StandardInput.WriteLine(input); return _testConsole.TotalProcessorTime.TotalSeconds < parser.TimeLimit; }); if (TimeLimitExceeded(parser.TimeLimit)) { watch.Stop(); _testConsole.Kill(); ReportThatTestTimedOut(result); } else { result.Status = GetProgramOutput() == parser.Expected ? ResultStatus.Passed : ResultStatus.Failed; watch.Stop(); } } catch (Exception) { result.Status = ResultStatus.Exception; } result.Elapsed = watch.Elapsed; return result; } 

_testConsole is a process adapter that wraps a regular .net process with something more efficient. However, it is difficult for me to find exceptions to the process that has begun (i.e., I am using something like:

 _process = new Process { StartInfo = { FileName = pathToProcess, UseShellExecute = false, CreateNoWindow = true, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, Arguments = arguments } }; 

to set up the process. Any ideas?

+3
source share
1 answer

Exceptions do not arise from one process to another. The best thing you could do is keep track of the process exit code - conditionally, exit code 0 represents success, and any other exit code represents an error.

Of course, the fact is that the case for the processes you started is another matter.

+12
source

All Articles