8. Robust Exception Handling & Custom Test Failures

Exception Handling Pattern for Automation

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()

Task

  • Implement safe_int_conversion(val: str, default: int = 0) -> int:
  • 1. In a try block, return int(val).
  • 2. Catch ValueError and return default.
πŸ’‘Common doubts
Did you like the problem?
Lesson 8 of 43
Python
Loading code editor…
Next