Selenium + Python · Lesson 8 of 43
Module 8: Exception Handling
Your task
Implement safe_int_conversion(val: str, default: int = 0) -> int:
Write it in the editor, then press Check answer (Ctrl + Enter).
class AutomationTimeoutError(Exception):
# Custom exception for test framework timeouts
pass
def execute_with_cleanup(action_fn, cleanup_fn):
try:
action_fn()
except Exception as e:
print(f"Action failed: {e}")
raise AutomationTimeoutError(f"Step failed: {e}") from e
finally:
cleanup_fn()Automation runs in dynamic web environments where elements can vanish and timeouts happen. In this lesson, you will master try, except, else, finally, and raising custom exceptions to catch and log errors gracefully. Let's master error handling!
Part of the Selenium + Python course (Selenium with Python). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
The 'else' block executes only if NO exception occurred inside the try block.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___