fork download
  1. from datetime import datetime
  2.  
  3. x = 'foo'
  4. try:
  5. age = int(x)
  6. except ValueError:
  7. try:
  8. age = datetime.now().year - datetime.fromisoformat(x).year
  9. except ValueError as e:
  10. raise ValueError(f'{x} is not a valid age or date.') from e
Runtime error #stdin #stdout #stderr 0.13s 26272KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 5, in <module>
ValueError: invalid literal for int() with base 10: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./prog.py", line 8, in <module>
ValueError: Invalid isoformat string: 'foo'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./prog.py", line 10, in <module>
ValueError: foo is not a valid age or date.