about try and exception

Shi Mu samrobertsmith at gmail.com
Fri Nov 18 01:33:15 CET 2005


On 11/17/05, Carl J. Van Arsdall <cvanarsdall at mvista.com> wrote:
> I would think that when the exception occurs the interpreter exits the
> block of code it is currently in and enters the exception block.
>
> Thus the line n = 1/2 would never get executed.
>
>
> -Carl
>
> Ben Bush wrote:
> > I wrote the following code to test the use of "try...exception",
> > and I want n to be printed out. However, the following code's output is:
> > Traceback (most recent call last):
> >   File
> > "C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
> > line 310, in RunScript
> >     exec codeObject in __main__.__dict__
> >   File "C:\py\use\tryExVa.py", line 7, in ?
> >     print n
> > NameError: name 'n' is not defined
> >
> > the code is here:
> >
> > try:
> >     m=2/0
> >     n=1/2
> > except ZeroDivisionError:
> >     pass
> > print "Yes!!! This line will always print"
> > print n
>
It is true. If I want to run the statement after the division error
such as n=1/2, what should I do?



More information about the Python-list mailing list