exceptions.nim 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const NimStackTraceMsgs =
  2. when defined(nimHasStacktraceMsgs): compileOption("stacktraceMsgs")
  3. else: false
  4. type
  5. RootEffect* {.compilerproc.} = object of RootObj ## \
  6. ## Base effect class.
  7. ##
  8. ## Each effect should inherit from `RootEffect` unless you know what
  9. ## you're doing.
  10. TimeEffect* = object of RootEffect ## Time effect.
  11. IOEffect* = object of RootEffect ## IO effect.
  12. ReadIOEffect* = object of IOEffect ## Effect describing a read IO operation.
  13. WriteIOEffect* = object of IOEffect ## Effect describing a write IO operation.
  14. ExecIOEffect* = object of IOEffect ## Effect describing an executing IO operation.
  15. StackTraceEntry* = object ## In debug mode exceptions store the stack trace that led
  16. ## to them. A `StackTraceEntry` is a single entry of the
  17. ## stack trace.
  18. procname*: cstring ## Name of the proc that is currently executing.
  19. line*: int ## Line number of the proc that is currently executing.
  20. filename*: cstring ## Filename of the proc that is currently executing.
  21. when NimStackTraceMsgs:
  22. frameMsg*: string ## When a stacktrace is generated in a given frame and
  23. ## rendered at a later time, we should ensure the stacktrace
  24. ## data isn't invalidated; any pointer into PFrame is
  25. ## subject to being invalidated so shouldn't be stored.
  26. when defined(nimStackTraceOverride):
  27. programCounter*: uint ## Program counter - will be used to get the rest of the info,
  28. ## when `$` is called on this type. We can't use
  29. ## "cuintptr_t" in here.
  30. procnameStr*, filenameStr*: string ## GC-ed objects holding the cstrings in "procname" and "filename"
  31. Exception* {.compilerproc, magic: "Exception".} = object of RootObj ## \
  32. ## Base exception class.
  33. ##
  34. ## Each exception has to inherit from `Exception`. See the full `exception
  35. ## hierarchy <manual.html#exception-handling-exception-hierarchy>`_.
  36. parent*: ref Exception ## Parent exception (can be used as a stack).
  37. name*: cstring ## The exception's name is its Nim identifier.
  38. ## This field is filled automatically in the
  39. ## ``raise`` statement.
  40. msg* {.exportc: "message".}: string ## The exception's message. Not
  41. ## providing an exception message
  42. ## is bad style.
  43. when defined(js):
  44. trace: string
  45. else:
  46. trace: seq[StackTraceEntry]
  47. up: ref Exception # used for stacking exceptions. Not exported!
  48. Defect* = object of Exception ## \
  49. ## Abstract base class for all exceptions that Nim's runtime raises
  50. ## but that are strictly uncatchable as they can also be mapped to
  51. ## a ``quit`` / ``trap`` / ``exit`` operation.
  52. CatchableError* = object of Exception ## \
  53. ## Abstract class for all exceptions that are catchable.
  54. IOError* = object of CatchableError ## \
  55. ## Raised if an IO error occurred.
  56. EOFError* = object of IOError ## \
  57. ## Raised if an IO "end of file" error occurred.
  58. OSError* = object of CatchableError ## \
  59. ## Raised if an operating system service failed.
  60. errorCode*: int32 ## OS-defined error code describing this error.
  61. LibraryError* = object of OSError ## \
  62. ## Raised if a dynamic library could not be loaded.
  63. ResourceExhaustedError* = object of CatchableError ## \
  64. ## Raised if a resource request could not be fulfilled.
  65. ArithmeticDefect* = object of Defect ## \
  66. ## Raised if any kind of arithmetic error occurred.
  67. DivByZeroDefect* = object of ArithmeticDefect ## \
  68. ## Raised for runtime integer divide-by-zero errors.
  69. OverflowDefect* = object of ArithmeticDefect ## \
  70. ## Raised for runtime integer overflows.
  71. ##
  72. ## This happens for calculations whose results are too large to fit in the
  73. ## provided bits.
  74. AccessViolationDefect* = object of Defect ## \
  75. ## Raised for invalid memory access errors
  76. AssertionDefect* = object of Defect ## \
  77. ## Raised when assertion is proved wrong.
  78. ##
  79. ## Usually the result of using the `assert() template
  80. ## <assertions.html#assert.t,untyped,string>`_.
  81. ValueError* = object of CatchableError ## \
  82. ## Raised for string and object conversion errors.
  83. KeyError* = object of ValueError ## \
  84. ## Raised if a key cannot be found in a table.
  85. ##
  86. ## Mostly used by the `tables <tables.html>`_ module, it can also be raised
  87. ## by other collection modules like `sets <sets.html>`_ or `strtabs
  88. ## <strtabs.html>`_.
  89. OutOfMemDefect* = object of Defect ## \
  90. ## Raised for unsuccessful attempts to allocate memory.
  91. IndexDefect* = object of Defect ## \
  92. ## Raised if an array index is out of bounds.
  93. FieldDefect* = object of Defect ## \
  94. ## Raised if a record field is not accessible because its discriminant's
  95. ## value does not fit.
  96. RangeDefect* = object of Defect ## \
  97. ## Raised if a range check error occurred.
  98. StackOverflowDefect* = object of Defect ## \
  99. ## Raised if the hardware stack used for subroutine calls overflowed.
  100. ReraiseDefect* = object of Defect ## \
  101. ## Raised if there is no exception to reraise.
  102. ObjectAssignmentDefect* = object of Defect ## \
  103. ## Raised if an object gets assigned to its parent's object.
  104. ObjectConversionDefect* = object of Defect ## \
  105. ## Raised if an object is converted to an incompatible object type.
  106. ## You can use ``of`` operator to check if conversion will succeed.
  107. FloatingPointDefect* = object of Defect ## \
  108. ## Base class for floating point exceptions.
  109. FloatInvalidOpDefect* = object of FloatingPointDefect ## \
  110. ## Raised by invalid operations according to IEEE.
  111. ##
  112. ## Raised by ``0.0/0.0``, for example.
  113. FloatDivByZeroDefect* = object of FloatingPointDefect ## \
  114. ## Raised by division by zero.
  115. ##
  116. ## Divisor is zero and dividend is a finite nonzero number.
  117. FloatOverflowDefect* = object of FloatingPointDefect ## \
  118. ## Raised for overflows.
  119. ##
  120. ## The operation produced a result that exceeds the range of the exponent.
  121. FloatUnderflowDefect* = object of FloatingPointDefect ## \
  122. ## Raised for underflows.
  123. ##
  124. ## The operation produced a result that is too small to be represented as a
  125. ## normal number.
  126. FloatInexactDefect* = object of FloatingPointDefect ## \
  127. ## Raised for inexact results.
  128. ##
  129. ## The operation produced a result that cannot be represented with infinite
  130. ## precision -- for example: ``2.0 / 3.0, log(1.1)``
  131. ##
  132. ## **Note**: Nim currently does not detect these!
  133. DeadThreadDefect* = object of Defect ## \
  134. ## Raised if it is attempted to send a message to a dead thread.
  135. NilAccessDefect* = object of Defect ## \
  136. ## Raised on dereferences of ``nil`` pointers.
  137. ##
  138. ## This is only raised if the `segfaults module <segfaults.html>`_ was imported!
  139. ArithmeticError* {.deprecated: "See corresponding Defect".} = ArithmeticDefect
  140. DivByZeroError* {.deprecated: "See corresponding Defect".} = DivByZeroDefect
  141. OverflowError* {.deprecated: "See corresponding Defect".} = OverflowDefect
  142. AccessViolationError* {.deprecated: "See corresponding Defect".} = AccessViolationDefect
  143. AssertionError* {.deprecated: "See corresponding Defect".} = AssertionDefect
  144. OutOfMemError* {.deprecated: "See corresponding Defect".} = OutOfMemDefect
  145. IndexError* {.deprecated: "See corresponding Defect".} = IndexDefect
  146. FieldError* {.deprecated: "See corresponding Defect".} = FieldDefect
  147. RangeError* {.deprecated: "See corresponding Defect".} = RangeDefect
  148. StackOverflowError* {.deprecated: "See corresponding Defect".} = StackOverflowDefect
  149. ReraiseError* {.deprecated: "See corresponding Defect".} = ReraiseDefect
  150. ObjectAssignmentError* {.deprecated: "See corresponding Defect".} = ObjectAssignmentDefect
  151. ObjectConversionError* {.deprecated: "See corresponding Defect".} = ObjectConversionDefect
  152. FloatingPointError* {.deprecated: "See corresponding Defect".} = FloatingPointDefect
  153. FloatInvalidOpError* {.deprecated: "See corresponding Defect".} = FloatInvalidOpDefect
  154. FloatDivByZeroError* {.deprecated: "See corresponding Defect".} = FloatDivByZeroDefect
  155. FloatOverflowError* {.deprecated: "See corresponding Defect".} = FloatOverflowDefect
  156. FloatUnderflowError* {.deprecated: "See corresponding Defect".} = FloatUnderflowDefect
  157. FloatInexactError* {.deprecated: "See corresponding Defect".} = FloatInexactDefect
  158. DeadThreadError* {.deprecated: "See corresponding Defect".} = DeadThreadDefect
  159. NilAccessError* {.deprecated: "See corresponding Defect".} = NilAccessDefect