Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

invalid_exceptions_caught.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # pylint: disable=missing-docstring, too-few-public-methods
  2. # pylint: disable=too-many-ancestors, no-absolute-import, import-error, multiple-imports,wrong-import-position
  3. from __future__ import print_function
  4. import socket, binascii, abc, six
  5. class MyException(object):
  6. """Custom 'exception'."""
  7. class MySecondException(object):
  8. """Custom 'exception'."""
  9. class MyGoodException(Exception):
  10. """Custom exception."""
  11. class MySecondGoodException(MyGoodException):
  12. """Custom exception."""
  13. class SkipException(socket.error):
  14. """Not an exception for Python 2, but one in 3."""
  15. class SecondSkipException(SkipException):
  16. """Also a good exception."""
  17. try:
  18. 1 + 1
  19. except MyException: # [catching-non-exception]
  20. print("caught")
  21. try:
  22. 1 + 2
  23. # +1:[catching-non-exception,catching-non-exception]
  24. except (MyException, MySecondException):
  25. print("caught")
  26. try:
  27. 1 + 3
  28. except MyGoodException:
  29. print("caught")
  30. try:
  31. 1 + 3
  32. except (MyGoodException, MySecondGoodException):
  33. print("caught")
  34. try:
  35. 1 + 3
  36. except (SkipException, SecondSkipException):
  37. print("caught")
  38. try:
  39. 1 + 42
  40. # +1:[catching-non-exception,catching-non-exception]
  41. except (None, list()):
  42. print("caught")
  43. try:
  44. 1 + 24
  45. except None: # [catching-non-exception]
  46. print("caught")
  47. EXCEPTION = None
  48. EXCEPTION = ZeroDivisionError
  49. try:
  50. 1 + 46
  51. except EXCEPTION:
  52. print("caught")
  53. try:
  54. 1 + 42
  55. # +1:[catching-non-exception,catching-non-exception,catching-non-exception]
  56. except (list([4, 5, 6]), None, ZeroDivisionError, 4):
  57. print("caught")
  58. EXCEPTION_TUPLE = (ZeroDivisionError, OSError)
  59. NON_EXCEPTION_TUPLE = (ZeroDivisionError, OSError, 4)
  60. try:
  61. 1 + 42
  62. except EXCEPTION_TUPLE:
  63. print("caught")
  64. try:
  65. 1 + 42
  66. except NON_EXCEPTION_TUPLE: # [catching-non-exception]
  67. print("caught")
  68. from missing_import import UnknownError
  69. UNKNOWN_COMPONENTS = (ZeroDivisionError, UnknownError)
  70. try:
  71. 1 + 42
  72. except UNKNOWN_COMPONENTS:
  73. print("caught")
  74. try:
  75. 1 + 42
  76. except binascii.Error:
  77. print('builtin and detected')
  78. try:
  79. 1 + 45
  80. except object: # [catching-non-exception]
  81. print('caught')
  82. try:
  83. 1 + 42
  84. except range: # [catching-non-exception]
  85. print('caught')
  86. class HasErrorInMRO(six.with_metaclass(abc.ABCMeta, Exception)):
  87. pass
  88. class Second(HasErrorInMRO):
  89. pass
  90. try:
  91. raise Second
  92. except Second:
  93. pass
  94. class SomeBase(UnknownError):
  95. pass
  96. EXCEPTIONS = (SomeBase, ValueError)
  97. try:
  98. raise ValueError
  99. except EXCEPTIONS:
  100. pass