try:
risky_operation()
except Exception as e:
print("Error:", e)
try:
value = int("xyz")
except ValueError:
print("Logging error before re-raising")
raise
class InvalidAgeError(Exception):
pass
def validate_age(age):
if age < 18:
raise InvalidAgeError("Age must be 18 or above")
try:
validate_age(16)
except InvalidAgeError as e:
print(e)
def safe_divide(a, b):
try:
return a / b
except ZeroDivisionError as e:
return f"Error: {e}"
except Exception as e:
return f"Unexpected error: {e}"
finally:
print("Operation attempted")
print(safe_divide(10, 0))