Quantcast
Channel: Why does using `or` within an except clause not cause a SyntaxError? Is there a valid use for it? - Stack Overflow
Browsing all 3 articles
Browse latest View live

Answer by user2622016 for Why does using `or` within an except clause not...

You should use a n-tuple of types instead of a logical expression (which just returns the first non-false element):def with_or_raise(exc): try: raise exc() except (IndexError,KeyError): print('Got ya!')

View Article



Answer by deceze for Why does using `or` within an except clause not cause a...

In except e, e can be any valid Python expression:try1_stmt ::= "try"":" suite ("except" [expression ["as" identifier]] ":" suite)+ ...[..] For an except clause with an expression, that expression is...

View Article

Why does using `or` within an except clause not cause a SyntaxError? Is there...

At work, I stumbled upon an except clause with an or operator:try: # Do something.except IndexError or KeyError: # ErrorHandlingI know the exception classes should be passed as a tuple, but it bugged...

View Article
Browsing all 3 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>