Try block problem

Jeff Davis jdavis at empires.org
Tue Nov 20 03:56:39 EST 2001


I am not sure you understood my question. I wanted to know why the 
"finally" could not be used with "try", i.e. why did the developers of 
python choose to make the language that way.

Thanks,
        Jeff

Greg Krohn wrote:

> "Jeff Davis" <jdavis at empires.org> wrote in message
> news:9tcnls$42k$1 at news1.ucsd.edu...
>>         It seems like the following makes sense to me:
>>
>> try:
>>         ...
>> except:
>>         ...
>> finally:
>>         ...
> 
> except and finally can't be used together.
> 
>>>> def spam(x):
> ...  try:
> ...   print 10 / x
> ...  finally:
> ...   print 'Cleanup before exception is raised'
> ...
>>>> def eggs(x):
> ...  try:
> ...   print 10 / x
> ...  except:
> ...   print 'Bad x! Bad, bad x!'
> ...
>>>> spam(1)
> 10
> Cleanup before exception is raised
>>>> spam(0)
> Cleanup before exception is raised
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
>   File "<interactive input>", line 3, in spam
> ZeroDivisionError: integer division or modulo by zero
>>>> eggs(1)
> 10
>>>> eggs(0)
> Bad x! Bad, bad x!
> 
> More info: http://www.python.org/doc/current/ref/try.html
> 
> greg




More information about the Python-list mailing list