gpt4 book ai didi

exception - 在 C# 中捕获 Ironpython 异常

转载 作者:行者123 更新时间:2023-12-04 13:10:29 25 4
gpt4 key购买 nike

我在 C# 中嵌入 IronPython 2.0。在 IronPython 中,我定义了自己的异常:

def foobarException(Exception):
pass

并在某处提出:

raise foobarException( "This is the Exception Message" )

现在在 C# 中,我有:

try
{
callIronPython();
}
catch (Exception e)
{
// How can I determine the name (foobarException) of the Exception
// that is thrown from IronPython?
// With e.Message, I get "This is the Exception Message"
}

最佳答案

当您从 C# 捕获 IronPython 异常时,您可以使用 Python 引擎来格式化回溯:

catch (Exception e)
{
ExceptionOperations eo = _engine.GetService<ExceptionOperations>();
string error = eo.FormatException(e);
Console.WriteLine(error);
}

您可以从回溯中提取异常名称。否则,您将不得不调用 IronPython 托管 API 以直接从异常实例中检索信息。 engine.Operations有这些类型的交互有用的方法。

关于exception - 在 C# 中捕获 Ironpython 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/614365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com