[IronPython] .NET Exception to Python Exception conversion

Michael Foord fuzzyman at voidspace.org.uk
Wed Jun 20 00:52:26 CEST 2007


Cool - thanks. :-)

Michael

Dino Viehland wrote:
> It's a little bit different, and you don't need to create the PythonEngine (or at least until we close on that issue :) ), but it's:
>
> import clr
> clr.AddReference('IronPython')
> from IronPython.Hosting import PythonEngine
> PythonEngine.CurrentEngine.FormatException(someException)
>
>
> We no longer automatically add references to IronPython's dlls which is why you need the AddReference step as well.
>
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Tuesday, June 19, 2007 3:45 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] .NET Exception to Python Exception conversion
>
> Dino Viehland wrote:
>   
>> If the exception was raised in Python then you should be able to do:
>>
>> print event.Exception.Data['PythonExceptionInfo']
>>
>> which will contain the Python exception object, not that I'm sure you
>> can format that one much better.
>>
>> What you probably want to do is call
>> PythonEngine.FormatException(event.Exception), for example:
>>
>> from IronPython.Hosting import PythonEngine
>>
>> x = PythonEngine()
>>
>> print x.FormatException(event.Exception)
>>
>>     
>
> That works! Can you do it with IronPython 2 ? ;-)
>
> Michael Foord
>
>
>   
>> If you're hosting IronPython then you can use your previously
>> instantiated PythonEngine instead of newing one up just to format an
>> exception.
>>
>> *From:* users-bounces at lists.ironpython.com
>> [mailto:users-bounces at lists.ironpython.com] *On Behalf Of *Laurent
>> Debacker
>> *Sent:* Tuesday, June 19, 2007 11:45 AM
>> *To:* users at lists.ironpython.com
>> *Subject:* [IronPython] .NET Exception to Python Exception conversion
>>
>> Hello,
>>
>> I'm catching exceptions raised in my winforms thread using
>>
>> def exceptionHandler(sender, event):
>> print event.Exception
>>
>> Application.ThreadException +=
>> ThreadExceptionEventHandler(exceptionHandler)
>> Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
>>
>> However the exception I get, contains the long, unintuitive stack
>> trace. How can I convert it to a nice Python exception ?
>>
>> Regards,
>> Laurent Debacker.
>>
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> users mailing list
>> users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>     
>
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> users mailing list
> users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>   




More information about the Ironpython-users mailing list