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" )
Now in C#, I have:
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"
}
When you catch an IronPython exception from C#, you can use the Python engine to format the traceback:
You can pull the exception name out of the traceback. Otherwise, you will have to call into the IronPython hosting API to retrieve information directly from the exception instance.
engine.Operations
has useful methods for these kinds of interactions.The way IronPython maps .NET exceptions to Python ones isn't always straightforward; a lot of exceptions are reported as
SystemError
(although if you import the .NET exception type you can specify that in theexcept
clause). You can get the Python type of the exception usingIf you want the .NET exception type, make sure you have
import clr
in the module. It makes .NET attributes available on objects, like theToUpper()
method on strings. Then you can access the .NET exception using the.clsException
attribute:prints:
An example of catching the specific .NET exception you want:
My final solution was:
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:
Assuming that you compiled your python code with the .NET equivalent compiler you would have a static type which is just that exception. If this exception is public (exported type) then you reference the assembly that contains your python code in your project and dig up the type foobarException in some python namespace. That way C# will be able to type match that exception. This is the only way you can do that properly.