Quantcast
Channel: Why does using `or` within an except clause not cause a SyntaxError? Is there a valid use for it? - Stack Overflow
Browsing index pages (3 articles)

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


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


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
Browsing index pages (3 articles)


Latest Images