[IPython-User] trapping error thrown by line magics?

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

[IPython-User] trapping error thrown by line magics?

Catherine Devlin
I really enjoy being able to build informal IPython scripts that include line magics.  Maybe too much.

When I do so, is there a way to trap errors thrown by line magics?  This doesn't work as I had hoped:
try:
    %cd directory_that_does_not_exist
except Exception as err:
    print('error trapped!')
[Errno 2] No such file or directory: 'directory_that_does_not_exist'
/home/catherine/werk/openFEC/data/tuning

Thanks,

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user
Reply | Threaded
Open this post in threaded view
|

Re: trapping error thrown by line magics?

Fernando Perez
Dear Catherine,

certain magics swallow their exceptions, %cd in particular.  We made that decision to make them more human-friendly, since magics were originally conceived really as a pure human-driven convenience while working interactively, not as a library/programming construct.

In general, the magic system does *not* swallow all exceptions, though, so you could add your own versions of the ones you need with different behavior.

Not an ideal answer, but at least the system doesn't intrinsically prevent what you're looking for (which I was afraid we might, so I actually went looking a bit).

Cheers

f


On Tue, Jan 6, 2015 at 8:42 AM, Catherine Devlin <[hidden email]> wrote:
I really enjoy being able to build informal IPython scripts that include line magics.  Maybe too much.

When I do so, is there a way to trap errors thrown by line magics?  This doesn't work as I had hoped:
try:
    %cd directory_that_does_not_exist
except Exception as err:
    print('error trapped!')
[Errno 2] No such file or directory: 'directory_that_does_not_exist'
/home/catherine/werk/openFEC/data/tuning

Thanks,

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user




--
Fernando Perez (@fperez_org; http://fperez.org)
fperez.net-at-gmail: mailing lists only (I ignore this when swamped!)
fernando.perez-at-berkeley: contact me here for any direct mail

_______________________________________________
IPython-User mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/ipython-user