Exception handler

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Exception handler

Thomas Graff
Hello Enthought community


I try to add a custom top level exception handler to my application.
But I am not able to capture all exceptions.

from traits.etsconfig.api import ETSConfig
# ETSConfig.toolkit = 'wx'
ETSConfig.toolkit = 'qt4'

from traits.api import push_exception_handler

def eh(o, t, ov, nv):
    print("Something unexpected happend")

push_exception_handler(eh)

import traits.api as _traits
import traitsui.api as _traitsui

class MH(_traitsui.Handler):

    def plop(self, info):
        1/0

    def object_plip_changed(self, info):
        1/0


class MainUi(_traits.HasTraits):
    plip = _traits.Button('Plip')

    import_action = _traitsui.Action(name = '&Plop', action = 'plop')
    exit_action = _traitsui.Action(name='E&xit', action='_on_close')

    traits_view = _traitsui.View(
        _traitsui.UItem('plip'),
        resizable = True,
        menubar = _traitsui.MenuBar(
            _traitsui.Menu(import_action, exit_action, name='&File'),
            ),
        handler = MH
        )

if __name__ == '__main__':
    mu = MainUi()
    mu.configure_traits()


In my example code, the Plip event exception is captured by eh()
handler, but the Plop event exception from the menu is not captured by
eh().

Any suggestion for how i can solve this?

I am running ETS traits v4.2.0 on linux.


Thanks,
Thomas Graff
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exception handler

Robert Kern
On Thu, Jul 11, 2013 at 12:03 PM, Thomas Graff <[hidden email]> wrote:

> Hello Enthought community
>
>
> I try to add a custom top level exception handler to my application.
> But I am not able to capture all exceptions.
>
> from traits.etsconfig.api import ETSConfig
> # ETSConfig.toolkit = 'wx'
> ETSConfig.toolkit = 'qt4'
>
> from traits.api import push_exception_handler
>
> def eh(o, t, ov, nv):
>     print("Something unexpected happend")
>
> push_exception_handler(eh)
>
> import traits.api as _traits
> import traitsui.api as _traitsui
>
> class MH(_traitsui.Handler):
>
>     def plop(self, info):
>         1/0
>
>     def object_plip_changed(self, info):
>         1/0
>
>
> class MainUi(_traits.HasTraits):
>     plip = _traits.Button('Plip')
>
>     import_action = _traitsui.Action(name = '&Plop', action = 'plop')
>     exit_action = _traitsui.Action(name='E&xit', action='_on_close')
>
>     traits_view = _traitsui.View(
>         _traitsui.UItem('plip'),
>         resizable = True,
>         menubar = _traitsui.MenuBar(
>             _traitsui.Menu(import_action, exit_action, name='&File'),
>             ),
>         handler = MH
>         )
>
> if __name__ == '__main__':
>     mu = MainUi()
>     mu.configure_traits()
>
>
> In my example code, the Plip event exception is captured by eh()
> handler, but the Plop event exception from the menu is not captured by
> eh().
>
> Any suggestion for how i can solve this?

push_exception_handler() does not manage the unique top-level
exception handler. This is just the exception handler for Trait
notifications. If your exception happens outside of a Trait
notification, like plop(), it will bubble up to your GUI toolkit's
event loop, which usually catches it, prints out the stack and
continues. You will have to consult your GUI toolkit's documentation
on how to configure this.

--
Robert Kern
Enthought

On Thu, Jul 11, 2013 at 12:03 PM, Thomas Graff <[hidden email]> wrote:

> Hello Enthought community
>
>
> I try to add a custom top level exception handler to my application.
> But I am not able to capture all exceptions.
>
> from traits.etsconfig.api import ETSConfig
> # ETSConfig.toolkit = 'wx'
> ETSConfig.toolkit = 'qt4'
>
> from traits.api import push_exception_handler
>
> def eh(o, t, ov, nv):
>     print("Something unexpected happend")
>
> push_exception_handler(eh)
>
> import traits.api as _traits
> import traitsui.api as _traitsui
>
> class MH(_traitsui.Handler):
>
>     def plop(self, info):
>         1/0
>
>     def object_plip_changed(self, info):
>         1/0
>
>
> class MainUi(_traits.HasTraits):
>     plip = _traits.Button('Plip')
>
>     import_action = _traitsui.Action(name = '&Plop', action = 'plop')
>     exit_action = _traitsui.Action(name='E&xit', action='_on_close')
>
>     traits_view = _traitsui.View(
>         _traitsui.UItem('plip'),
>         resizable = True,
>         menubar = _traitsui.MenuBar(
>             _traitsui.Menu(import_action, exit_action, name='&File'),
>             ),
>         handler = MH
>         )
>
> if __name__ == '__main__':
>     mu = MainUi()
>     mu.configure_traits()
>
>
> In my example code, the Plip event exception is captured by eh()
> handler, but the Plop event exception from the menu is not captured by
> eh().
>
> Any suggestion for how i can solve this?
>
> I am running ETS traits v4.2.0 on linux.
>
>
> Thanks,
> Thomas Graff
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev



--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exception handler

Thomas Graff
Thanks, for the clarification.

Looks like implementing my own sys.excepthook pretty much does what I want.


Thomas Graff

2013/7/11 Robert Kern <[hidden email]>:

> On Thu, Jul 11, 2013 at 12:03 PM, Thomas Graff <[hidden email]> wrote:
>> Hello Enthought community
>>
>>
>> I try to add a custom top level exception handler to my application.
>> But I am not able to capture all exceptions.
>>
>> from traits.etsconfig.api import ETSConfig
>> # ETSConfig.toolkit = 'wx'
>> ETSConfig.toolkit = 'qt4'
>>
>> from traits.api import push_exception_handler
>>
>> def eh(o, t, ov, nv):
>>     print("Something unexpected happend")
>>
>> push_exception_handler(eh)
>>
>> import traits.api as _traits
>> import traitsui.api as _traitsui
>>
>> class MH(_traitsui.Handler):
>>
>>     def plop(self, info):
>>         1/0
>>
>>     def object_plip_changed(self, info):
>>         1/0
>>
>>
>> class MainUi(_traits.HasTraits):
>>     plip = _traits.Button('Plip')
>>
>>     import_action = _traitsui.Action(name = '&Plop', action = 'plop')
>>     exit_action = _traitsui.Action(name='E&xit', action='_on_close')
>>
>>     traits_view = _traitsui.View(
>>         _traitsui.UItem('plip'),
>>         resizable = True,
>>         menubar = _traitsui.MenuBar(
>>             _traitsui.Menu(import_action, exit_action, name='&File'),
>>             ),
>>         handler = MH
>>         )
>>
>> if __name__ == '__main__':
>>     mu = MainUi()
>>     mu.configure_traits()
>>
>>
>> In my example code, the Plip event exception is captured by eh()
>> handler, but the Plop event exception from the menu is not captured by
>> eh().
>>
>> Any suggestion for how i can solve this?
>
> push_exception_handler() does not manage the unique top-level
> exception handler. This is just the exception handler for Trait
> notifications. If your exception happens outside of a Trait
> notification, like plop(), it will bubble up to your GUI toolkit's
> event loop, which usually catches it, prints out the stack and
> continues. You will have to consult your GUI toolkit's documentation
> on how to configure this.
>
> --
> Robert Kern
> Enthought
>
> On Thu, Jul 11, 2013 at 12:03 PM, Thomas Graff <[hidden email]> wrote:
>> Hello Enthought community
>>
>>
>> I try to add a custom top level exception handler to my application.
>> But I am not able to capture all exceptions.
>>
>> from traits.etsconfig.api import ETSConfig
>> # ETSConfig.toolkit = 'wx'
>> ETSConfig.toolkit = 'qt4'
>>
>> from traits.api import push_exception_handler
>>
>> def eh(o, t, ov, nv):
>>     print("Something unexpected happend")
>>
>> push_exception_handler(eh)
>>
>> import traits.api as _traits
>> import traitsui.api as _traitsui
>>
>> class MH(_traitsui.Handler):
>>
>>     def plop(self, info):
>>         1/0
>>
>>     def object_plip_changed(self, info):
>>         1/0
>>
>>
>> class MainUi(_traits.HasTraits):
>>     plip = _traits.Button('Plip')
>>
>>     import_action = _traitsui.Action(name = '&Plop', action = 'plop')
>>     exit_action = _traitsui.Action(name='E&xit', action='_on_close')
>>
>>     traits_view = _traitsui.View(
>>         _traitsui.UItem('plip'),
>>         resizable = True,
>>         menubar = _traitsui.MenuBar(
>>             _traitsui.Menu(import_action, exit_action, name='&File'),
>>             ),
>>         handler = MH
>>         )
>>
>> if __name__ == '__main__':
>>     mu = MainUi()
>>     mu.configure_traits()
>>
>>
>> In my example code, the Plip event exception is captured by eh()
>> handler, but the Plop event exception from the menu is not captured by
>> eh().
>>
>> Any suggestion for how i can solve this?
>>
>> I am running ETS traits v4.2.0 on linux.
>>
>>
>> Thanks,
>> Thomas Graff
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
>
>
> --
> Robert Kern
> Enthought
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Exception handler

Robert Kern
On Thu, Jul 11, 2013 at 9:18 PM, Thomas Graff <[hidden email]> wrote:
> Thanks, for the clarification.
>
> Looks like implementing my own sys.excepthook pretty much does what I want.

I recommend doing the following as well:

  push_exception_handler(lambda *args, **kwds: None, reraise_exceptions=True)

That will make sure that Traits reraises exceptions that it encounters
in notification handlers so they will bubble up to your excepthook.

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev