【问题标题】:Exception Handling in C# to Python从 C# 到 Python 的异常处理
【发布时间】:2017-04-04 17:37:14
【问题描述】:

我有一个从 C# 调用的 python 脚本。下面提供的代码。此过程的问题是,如果 Python 脚本失败,我将无法在 C# 中理解并显示该异常。我正在使用 C#、MVC、Python。您能否修改以下代码并告诉我如何捕获 Python 脚本异常时抛出的异常?

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; 并更改 tis line : StreamReader myStreamReader = process.StandardOutput + process.StandardError; 以读取错误。
  • StreamReader myStreamReader = process.StandardOutput + process.StandardError;就像抛出编译错误一样。谢谢。
  • 嘿亚当 - 你的答案非常接近。我修改了代码并从 StandardError 获得了整个异常堆栈。我提供了以下代码的工作版本作为答案。感谢您的提示。 - 谢谢。

标签: c# python exception process exception-handling


【解决方案1】:

这里是工作代码。要在 Python 中获取 C# RedirectStandardError 属性 true 的错误或任何异常,然后获取标准错误。下面提供了代码的工作版本 -

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;

【讨论】:

    【解决方案2】:

    您可以订阅ErrorDataReceived事件并阅读错误信息。

    process.ErrorDataReceived+=process_ErrorDataReceived;
    

    这是你的事件处理程序

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

    【讨论】:

    • 谢谢。 ErrorDataReceived 为空。不幸的是,代码没有按预期工作。如果您发现任何其他选项,请告诉我。
    【解决方案3】:

    在使用 pythonnet 将 CPython 嵌入 C# 时,您可以获得从 Python 传递到 C# 的异常:

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

    【讨论】:

    • 谢谢。我发现已经找到了解决方案。我在下面发布了工作代码。再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 2011-04-27
    • 2011-09-11
    相关资源
    最近更新 更多