0

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";
     }
3
  • Add this line processStartInfo.RedirectStandardError = true; and change tis line : StreamReader myStreamReader = process.StandardOutput + process.StandardError; to read the error. Commented Apr 4, 2017 at 17:44
  • StreamReader myStreamReader = process.StandardOutput + process.StandardError; like is throwing a compilation Error. Thanks. Commented Apr 4, 2017 at 18:17
  • Hey Adam - Your answer was pretty close. I modified the code and got the entire exception stack from StandardError. I've provided the working version of the code below as answer. Thanks for the hints. - Thanks. Commented Apr 5, 2017 at 14:57

3 Answers 3

1

Here is the working code.. To get the error or any exception in Python to C# RedirectStandardError property true and next get the Standard Error. Working version of the code is provided below -

process.StartInfo = processStartInfo;
                        processStartInfo.RedirectStandardError = true;

                        stopWatch.Start();
                        //Start the process
                        process.Start();

                        string standardError = process.StandardError.ReadToEnd();

                        // wait exit signal from the app we called and then close it. 
                        process.WaitForExit();
                        process.Close();

                        stopWatch.Stop();
                        TimeSpan ts = stopWatch.Elapsed;
Sign up to request clarification or add additional context in comments.

Comments

0

You can subscribe for ErrorDataReceived event and read the error message.

process.ErrorDataReceived+=process_ErrorDataReceived;

and here is your event handler

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    Console.WriteLine(e.Data);
}

1 Comment

Thank you. ErrorDataReceived comes as null. Unfortunately code is not working as expected. Please let me know if you find any other option.
0

You can get exceptions passed from Python to C# when embedding CPython in C# with pythonnet:

https://github.com/pythonnet/pythonnet/blob/master/README.md

1 Comment

Thanks. I've found already found a solution. I posted working code below. Thank you again for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.