[Tutor] AssertionError issue

Alan Gauld alan.gauld at freenet.co.uk
Tue Jul 25 05:56:14 CEST 2006


> In a function of the class, I have put the code in try/except block.
>
> try:
>     ----------
>     ----------
> except AssertionError:
>     print "Error Condition"
>
> In this code, when error occurs, it raises the AssertionError
> but the destuctor isnt called itself.

The destructor will only get called if the object is destroyed.
The object will be destroyed once all references to it are broken
either by it moving out of scope (eg exiting a function/method)
or by you del()'ing it sufficient times to reduce its reference count
to zero..

> As a result the ssh conenctions are not closed.
> What is the general way a python script exits on encountering an 
> error?

raise SystemError or call sys,.exit() will exit completely.
But a try/finally (as opposed to a try/except) will also be useful to
guarantee execution of a block of code that can tidy up things
like open sockets/files etc.

HTH,

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list