fork download
  1. import sys
  2.  
  3. def uhoh(fallbacks):
  4. exception = sys.exc_value
  5. for exc, fallback in fallbacks:
  6. if isinstance(exception, exc):
  7. return fallback
  8.  
  9. try:
  10. a = 1 / 0
  11. except Exception as e:
  12. a = uhoh([(ZeroDivisionError, 0), (TypeError, 9)])
  13. print(a)
  14.  
  15. try:
  16. a = int('xyz')
  17. except Exception as e:
  18. a = uhoh([(ZeroDivisionError, 0), (ValueError, 9)])
  19. print(a)
Success #stdin #stdout 0.01s 7896KB
stdin
Standard input is empty
stdout
0
9