try-finally

How to determine if an exception was raised once you're in the finally block?

How to determine if an exception was raised once you're in the finally block? Question: Is it possible to tell if there was an exception once you’re in the finally clause? Something like: try: funky code finally: if ???: print(‘the funky code raised’) I’m looking to make something like this more DRY: try: funky code …

Total answers: 6

Python try finally block returns

Python try finally block returns Question: There is the interesting code below: def func1(): try: return 1 finally: return 2 def func2(): try: raise ValueError() except: return 1 finally: return 3 func1() func2() Could please somebody explain, what results will return these two functions and explain why, i.e. describe the order of the execution Asked …

Total answers: 4

Why do we need the "finally" clause in Python?

Why do we need the "finally" clause in Python? Question: I am not sure why we need finally in try…except…finally statements. In my opinion, this code block try: run_code1() except TypeError: run_code2() other_code() is the same with this one using finally: try: run_code1() except TypeError: run_code2() finally: other_code() Am I missing something? Asked By: RNA …

Total answers: 18

Python: Using continue in a try-finally statement in a loop

Python: Using continue in a try-finally statement in a loop Question: Will the following code: while True: try: print(“waiting for 10 seconds…”) continue print(“never show this”) finally: time.sleep(10) Always print the message “waiting for 10 seconds…”, sleep for 10 seconds, and do it again? In other words, do statements in finally clauses run even when …

Total answers: 3