exception-handling

"outsourcing" exception-handling to a decorator

"outsourcing" exception-handling to a decorator Question: Many try/except/finally-clauses not only “uglify” my code, but i find myself often using identical exception-handling for similar tasks. So i was considering reducing redundancy by “outsourcing” them to a … decorator. Because i was sure not to be the 1st one to come to this conclusion, I googled and …

Total answers: 3

try / else with return in try block

try / else with return in try block Question: I came across a strange behavior in python. I could not find information about this in the python help or on SE so here it is: def divide(x, y): print ‘entering divide’ try: return x/y except: print ‘error’ else: print ‘no error’ finally: print ‘exit’ print …

Total answers: 5

Catching all exceptions in Python

Catching all exceptions in Python Question: In Python, what’s the best way to catch “all” exceptions? except: # do stuff with sys.exc_info()[1] except BaseException as exc: except Exception as exc: The catch may be executing in a thread. My aim is to log any exception that might be thrown by normal code without masking any …

Total answers: 3

Catch any error in Python

Catch any error in Python Question: Is it possible to catch any error in Python? I don’t care what the specific exceptions will be, because all of them will have the same fallback. Asked By: user825286 || Source Answers: Using except by itself will catch any exception short of a segfault. try: something() except: fallback() …

Total answers: 8

Catch multiple exceptions in one line (except block)

Catch multiple exceptions in one line (except block) Question: I know that I can do: try: # do something that may fail except: # do this if ANYTHING goes wrong I can also do this: try: # do something that may fail except IDontLikeYouException: # say please except YouAreTooShortException: # stand on a ladder But …

Total answers: 6

Raising exceptions when an exception is already present in Python 3

Raising exceptions when an exception is already present in Python 3 Question: What happens to my first exception (A) when the second (B) is raised in the following code? class A(Exception): pass class B(Exception): pass try: try: raise A(‘first’) finally: raise B(‘second’) except X as c: print(c) If run with X = A I get: …

Total answers: 5

Logging uncaught exceptions in Python

Logging uncaught exceptions in Python Question: How do you cause uncaught exceptions to output via the logging module rather than to stderr? I realize the best way to do this would be: try: raise Exception, ‘Throwing a boring exception’ except Exception, e: logging.exception(e) But my situation is such that it would be really nice if …

Total answers: 10

Python: One Try Multiple Except

One try block with multiple excepts Question: In Python, is it possible to have multiple except statements for one try statement? Such as: try: #something1 #something2 except ExceptionType1: #return xyz except ExceptionType2: #return abc For the case of handling multiple exceptions the same way, see Catch multiple exceptions in one line (except block) Asked By: …

Total answers: 1