从except:block中捕获异常 [英] catching exceptions from an except: block
问题描述
大家好,
想象一下,我有三个函数a(x),b(x),c(x),每个函数返回
某事或者举一个例外。想象一下,我想定义一个函数
,如果可能的话返回一个(x),否则b(x),否则c(x),
否则会提高CantDoIt。
以下是我可以想到的三种方式:
----------
#这个看起来很难看
def nested_first(x):
试试:
返回一个(x)
除外:
试试:
返回b(x)
除外:
试试:
返回c(x)
除外:
提高CantDoIt
#这个看起来很啰嗦/>
def flat_first(x):
试试:
返回(x)
除外:
通过
试试:
返回b(x)
除外:
通过
试试:
返回c(x)
除外:
加上CantDoIt
#这个只能起作用,因为a,b,c是函数
#而且它似乎是对我的滥用循环构造
def rolled_first(x):<对于f,
在a,b,c:
尝试:
返回f(x)
除外:
继续
提高CantDoIt
----------
我对这些中的任何一个都不满意。有没有更令人满意的方式
在Python中这样做?我想要的是:
----------
#这个不正确但看起来最清晰给我
def wished_first(x):
试试:
返回(x)
除外:
返回b(x)
除外:
返回c(x)
除外:
提高CantDoIt
----------
我想我正在寻找的是某种
if:
elif:
....
elif:
else:
但是尝试:除了:
这就是为什么
尝试:
除外:
除了:
....
除了:
对我来说似乎很自然: )我想找到一个很好的方法来用
a语法正确的方式。
注意:我选择了函数a, b,c,但我真的在寻找一种适合任何代码块的方式
。
想象一下我有三个函数a(x),b(x),c(x)每个返回
$ b $某事或提出异常。想象一下我想定义一个函数
如果可能的话返回一个(x),否则b(x),否则c(x),
否则会提高CantDoIt。
例外是错误处理,而不是流量控制。
以下是我能想到的三种方式这样做:
...
#这只有效,因为a,b,c是函数
#此外,它似乎是对我的滥用循环构造
def rolled_first(x):
for a,b,c:
尝试:
返回f(x)
除外:
继续
加注CantDoIt
我的投票是针对那个。
我对任何一个都不满意这些。有没有更令人满意的方式
在Python中这样做?我想要的是:
----------
#这个不正确但看起来最清晰给我
def wished_first(x):
试试:
返回(x)
除外:
返回b(x)
除外:
返回c(x)
除外:
提高CantDoIt
同样,例外是错误处理,而不是流量控制。
作为旁注,尽量避免捕捉:,总是明确表示
异常。
HTH,
Miki< mi *********@gmail.com>
http:// pythonwise。 blogspot.com
3月7日,19:26,Miki < miki.teb ... @ gmail.comwrote:
Hello Arnaud,
Hi Miki
[snip]
例外是错误处理,而不是流量控制。
也许但并不总是那么明确!由于错误处理是流量控制的形式,因此两者必须在某处相遇。
[snip]
作为附注,尽量避免使用catch:,始终明确地说明
异常。
我没有说明我想要捕捉的东西,因为它并不觉得它与问题有关。
。 br />
谢谢
-
Arnaud
>
在< 11 ********************** @ 8g2000cwh.googlegroups.c om> ;, Arnaud Delobelle
写道:
#这个只能起作用,因为a,b,c是函数
#而且它似乎是滥用循环结构我
def rolled_first(x):
for f in a,b,c:
试试:
返回f(x)
除外:
继续
提高CantDoIt
-------- -
为什么你认为这是滥用?我认为这是完全有效的使用
a循环。
Ciao,
Marc''BlackJack''Rintsch
Hi all,
Imagine I have three functions a(x), b(x), c(x) that each return
something or raise an exception. Imagine I want to define a function
that returns a(x) if possible, otherwise b(x), otherwise c(x),
otherwise raise CantDoIt.
Here are three ways I can think of doing it:
----------
# This one looks ugly
def nested_first(x):
try:
return a(x)
except:
try:
return b(x)
except:
try:
return c(x)
except:
raise CantDoIt
# This one looks long-winded
def flat_first(x):
try:
return a(x)
except:
pass
try:
return b(x)
except:
pass
try:
return c(x)
except:
raise CantDoIt
# This one only works because a,b,c are functions
# Moreover it seems like an abuse of a loop construct to me
def rolled_first(x):
for f in a, b, c:
try:
return f(x)
except:
continue
raise CantDoIt
----------
I don''t feel happy with any of these. Is there a more satisfying way
of doing this in Python? What I would like is something like:
----------
# This one isn''t correct but looks the clearest to me
def wished_first(x):
try:
return a(x)
except:
return b(x)
except:
return c(x)
except:
raise CantDoIt
----------
I guess what I''m looking for is some sort of
if:
elif:
....
elif:
else:
but for try: except:
That''s why
try:
except:
except:
....
except:
seemed natural to me :) And I''d like to find a nice way to do this in
a syntactically correct way.
Note: I''ve chosen functions a, b, c, but really I''m looking for a way
that is suitable for any chunk of code.
Hello Arnaud,
Imagine I have three functions a(x), b(x), c(x) that each return
something or raise an exception. Imagine I want to define a function
that returns a(x) if possible, otherwise b(x), otherwise c(x),
otherwise raise CantDoIt.Exceptions are for error handling, not flow control.
Here are three ways I can think of doing it:
...
# This one only works because a,b,c are functions
# Moreover it seems like an abuse of a loop construct to me
def rolled_first(x):
for f in a, b, c:
try:
return f(x)
except:
continue
raise CantDoItMy vote is for that one.
I don''t feel happy with any of these. Is there a more satisfying way
of doing this in Python? What I would like is something like:
----------
# This one isn''t correct but looks the clearest to me
def wished_first(x):
try:
return a(x)
except:
return b(x)
except:
return c(x)
except:
raise CantDoItAgain, exception are for error handling, not for flow control.
As a side note, try to avoid "catch:", always catch explicit
exceptions.
HTH,
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
On 7 Mar, 19:26, "Miki" <miki.teb...@gmail.comwrote:Hello Arnaud,Hi Miki
[snip]Exceptions are for error handling, not flow control.Maybe but it''s not always that clear cut! As error handling is a form
of flow control the two have to meet somewhere.
[snip]As a side note, try to avoid "catch:", always catch explicit
exceptions.I didn''t specify what I wanted to catch because it didn''t feel it was
relevant to the problem.
Thanks
--
Arnaud
In <11**********************@8g2000cwh.googlegroups.c om>, Arnaud Delobelle
wrote:
# This one only works because a,b,c are functions
# Moreover it seems like an abuse of a loop construct to me
def rolled_first(x):
for f in a, b, c:
try:
return f(x)
except:
continue
raise CantDoIt
----------Why do you think this is an abuse? I think it''s a perfectly valid use of
a loop.
Ciao,
Marc ''BlackJack'' Rintsch
这篇关于从except:block中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!