在C#中捕获Ironpython异常 [英] Catching Ironpython Exception in C#

查看:115
本文介绍了在C#中捕获Ironpython异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将IronPython 2.0嵌入C#中。在IronPython中,我用以下方式定义了自己的异常:

I'm embedding IronPython 2.0 in C#. In IronPython, I defined my own exception with:

def foobarException(Exception):
    pass 

并将其提高到某个位置:

and raise it somewhere with:

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代码。在Ironpython中,我用我所有的计算值填充结果类。我向此类添加了一个成员变量IronPythonExceptionName。现在,我可以在IronPython中进行简单的尝试:

I have a result class in C# which is passed to my ironpython code. In Ironpython I fill the result class with all my computed values. To this class I just added a member variable IronPythonExceptionName. Now I just have a simple try in IronPython:

try: 
    complicatedIronPythonFunction()
except Exception, inst:
    result.IronPythonExceptionName = inst.__class__.__name__
    raise inst

这篇关于在C#中捕获Ironpython异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆