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

Using a contextmanager You could use a custom contextmanager, for example: class DidWeRaise: __slots__ = (‘exception_happened’, ) # instances will take less memory def __enter__(self): return self def __exit__(self, exc_type, exc_val, exc_tb): # If no exception happened the `exc_type` is None self.exception_happened = exc_type is not None And then use that inside the try: try: … Read more

SQL Server 2000: How to exit a stored procedure?

You can use RETURN to stop execution of a stored procedure immediately. Quote taken from Books Online: Exits unconditionally from a query or procedure. RETURN is immediate and complete and can be used at any point to exit from a procedure, batch, or statement block. Statements that follow RETURN are not executed. Out of paranoia, … Read more

How to exit an if clause

(This method works for ifs, multiple nested loops and other constructs that you can’t break from easily.) Wrap the code in its own function. Instead of break, use return. Example: def some_function(): if condition_a: # do something and return early … return … if condition_b: # do something else and return early … return … … Read more

How to avoid “if” chains?

You can use an && (logic AND): if (executeStepA() && executeStepB() && executeStepC()){ … } executeThisFunctionInAnyCase(); this will satisfy both of your requirements: executeStep<X>() should evaluate only if the previous one succeeded (this is called short circuit evaluation) executeThisFunctionInAnyCase() will be executed in any case