I've a python script that I'm invoking from C#. Code Provided below. Issue with this process is that if Python script fails I'm not able to understand in C# and display that exception. I'm using C#, MVC, Python. Can you please modify below code and show me how can I catch the exception thrown at the time of Python Script exception?
Process process = new Process();
Stopwatch stopWatch = new Stopwatch();
ProcessStartInfo processStartInfo = new ProcessStartInfo(python);
processStartInfo.UseShellExecute = false;
processStartInfo.RedirectStandardOutput = true;
try
{
process.StartInfo = processStartInfo;
stopWatch.Start();
//Start the process
process.Start();
// Read the standard output of the app we called.
// in order to avoid deadlock we will read output first
// and then wait for process terminate:
StreamReader myStreamReader = process.StandardOutput;
string myString = myStreamReader.ReadLine();
// wait exit signal from the app we called and then close it.
process.WaitForExit();
process.Close();
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Session["Message"] = "Success";
}
catch (InvalidOperationException ex)
{
throw new System.InvalidOperationException("Bulk Upload Failed. Please contact administrator for further details." + ex.StackTrace);
Session["Message"] = "Failed";
}
processStartInfo.RedirectStandardError = true;and change tis line :StreamReader myStreamReader = process.StandardOutput + process.StandardError;to read the error.