Guest User

Untitled

a guest
Feb 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. """ Test script for the HandlerChain.
  2.  
  3. From here we can see the output exception after HandlerChain handled.
  4. """
  5. from exceptiongroup import HandlerChain, ExceptionGroup
  6.  
  7. chain = HandlerChain()
  8.  
  9.  
  10. def raise_error_with_context():
  11. raise ZeroDivisionError("Fake zero division error")
  12.  
  13.  
  14. with chain:
  15.  
  16. try:
  17. raise RuntimeError("Runtime error")
  18. except RuntimeError as e:
  19. e1 = e
  20.  
  21. try:
  22. raise ValueError("ValueError error2")
  23. except ValueError as e:
  24. e2 = e
  25.  
  26. @chain.handle(RuntimeError)
  27. def handler2(exc):
  28. raise exc
  29.  
  30. raise ExceptionGroup("exception group", [e1, e2], [str(e1), str(e2)])
  31.  
  32.  
  33. # Traceback (most recent call last):
  34. # File "handler.py", line 30, in <module>
  35. # raise ExceptionGroup("exception group", [e1, e2], [str(e1), str(e2)])
  36. # File "/Users/chenhongze/projects/exceptiongroup/exceptiongroup/_tools.py", line 147, in __exit__
  37. # raise group
  38. # exceptiongroup.ExceptionGroup: <ExceptionGroup: RuntimeError('Runtime error')>, <ExceptionGroup: ValueError('ValueError error2')>
  39.  
  40. # RuntimeError('Runtime error'):
  41.  
  42. # Traceback (most recent call last):
  43. # File "handler.py", line 30, in <module>
  44. # raise ExceptionGroup("exception group", [e1, e2], [str(e1), str(e2)])
  45. # exceptiongroup.ExceptionGroup: RuntimeError('Runtime error')
  46.  
  47. # Runtime error:
  48.  
  49. # Traceback (most recent call last):
  50. # File "handler.py", line 17, in <module>
  51. # raise RuntimeError("Runtime error")
  52. # RuntimeError: Runtime error
  53.  
  54. # ValueError('ValueError error2'):
  55.  
  56. # Traceback (most recent call last):
  57. # File "handler.py", line 30, in <module>
  58. # raise ExceptionGroup("exception group", [e1, e2], [str(e1), str(e2)])
  59. # exceptiongroup.ExceptionGroup: ValueError('ValueError error2')
  60.  
  61. # ValueError error2:
  62.  
  63. # Traceback (most recent call last):
  64. # File "handler.py", line 22, in <module>
  65. # raise ValueError("ValueError error2")
  66. # ValueError: ValueError error2
Add Comment
Please, Sign In to add comment