Why no try-except-finally ?

Daniel Klein danielk at aracnet.com
Sun Nov 9 17:20:52 CET 2003


Shame, shame...it doesn't look like you Google'd enough
http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=20000708194536.O26534%40xs4all.nl&rnum=2&prev=/groups%3Fq%3Dgroup:comp.lang.python%2Binsubject:What%2Binsubject:about%2Binsubject:try:except:finally%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26as_drrb%3Db%26as_mind%3D12%26as_minm%3D5%26as_miny%3D2000%26as_maxd%3D9%26as_maxm%3D11%26as_maxy%3D2000%26selm%3D20000708194536.O26534%2540xs4all.nl%26rnum%3D2

Dan ;-)

On 05 Nov 2003 09:05:56 GMT, keflimarcusx at aol.comNOSPAM (KefX) wrote:

>This may have been discussed before, but I'm kind of confused as to why Python
>doesn't support having both an except ~and~ a finally clause, like this:
>
>try:
>    raise RuntimeException
>except:
>    print "What is the answer?"
>finally:
>    print 42
>
>This doesn't work because I use both 'except' and 'finally'. I'm not saying
>that it SHOULD work, I'm wondering why it doesn't. I thought about it for a
>second, though, and came up with the ad-hoc "nested-try idiom", as I call it:
>
>try:
>    try:
>        raise RuntimeException
>    except:
>        print "What is the answer?"
>finally:
>    print 42
>
>This works as expected: the exception gets thrown, the question gets asked in
>the 'except' clause, and then the question gets answered in the 'finally'
>clause. Of course in real-world code we wouldn't be asking questions and giving
>answers via exception-handling, but the path of execution is what counts. Is
>this idiom unPythonic somehow? (The nested-try thing does look odd...) Or is it
>the way we're supposed to do it? Or is there something I'm missing?
>
>- Kef





More information about the Python-list mailing list