# Re-raise the current exceptiondefprocess(data):
try:
returnint(data)
except ValueError:
print(f"Warning: could not process {data!r}")
raise# re-raise the same exceptiontry:
process("abc")
except ValueError as e:
print(f"Caught re-raised: {e}")
# Exception chaining with'from'classAppError(Exception):
passdefload_config(path):
try:
raise FileNotFoundError(f"No such file: {path}")
except FileNotFoundError as e:
raise AppError("Configuration failed") from e
try:
load_config("config.yaml")
except AppError as e:
print(f"Error: {e}")
print(f"Caused by: {e.__cause__}")
Output
Click "Run" to execute your code
Bare 'raise' re-throws the current exception. 'raise X from Y' chains exceptions, setting __cause__ for traceback clarity.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?