1
0

Kernel-Exceptions.st 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. Smalltalk createPackage: 'Kernel-Exceptions'!
  2. Object subclass: #Error
  3. instanceVariableNames: 'messageText'
  4. package: 'Kernel-Exceptions'!
  5. !Error commentStamp!
  6. From the ANSI standard:
  7. This protocol describes the behavior of instances of class `Error`.
  8. These are used to represent error conditions that prevent the normal continuation of processing.
  9. Actual error exceptions used by an application may be subclasses of this class.
  10. As `Error` is explicitly specified to be subclassable, conforming implementations must implement its behavior in a non-fragile manner.!
  11. !Error methodsFor: 'accessing'!
  12. beHandled
  13. <inlineJS: 'self.amberHandled = true'>
  14. !
  15. beUnhandled
  16. <inlineJS: 'self.amberHandled = false'>
  17. !
  18. context
  19. <inlineJS: 'return self.context'>
  20. !
  21. jsStack
  22. <inlineJS: 'return self.stack'>
  23. !
  24. messageText
  25. ^ messageText
  26. !
  27. messageText: aString
  28. messageText := aString
  29. !
  30. signalerContext
  31. ^ self signalerContextFrom: self context
  32. !
  33. signalerContextFrom: aContext
  34. "Find the first sender of signal(:), the first context which is neither
  35. for an instance method nor for a class side method of Exception (or subclass).
  36. This will make sure that the same context is found for both, `Error signal`
  37. and `Error new signal`"
  38. ^ aContext findContextSuchThat: [ :context |
  39. (context receiver == self
  40. or: [ context receiver == self class ]) not ]
  41. ! !
  42. !Error methodsFor: 'initialization'!
  43. initialize
  44. self messageText: 'Errorclass: ', (self class name).
  45. ! !
  46. !Error methodsFor: 'signaling'!
  47. resignal
  48. "Resignal the receiver without changing its exception context"
  49. <inlineJS: '
  50. self.amberHandled = false;
  51. throw(self);
  52. '>
  53. !
  54. signal
  55. <inlineJS: '
  56. self.amberHandled = false;
  57. self.context = $core.getThisContext();
  58. self.smalltalkError = true;
  59. throw self;
  60. '>
  61. !
  62. signal: aString
  63. self messageText: aString.
  64. self signal
  65. ! !
  66. !Error methodsFor: 'testing'!
  67. isSmalltalkError
  68. <inlineJS: 'return self.smalltalkError === true'>
  69. !
  70. wasHandled
  71. <inlineJS: 'return self.amberHandled || false'>
  72. ! !
  73. !Error class methodsFor: 'accessing'!
  74. classTag
  75. "Returns a tag or general category for this class.
  76. Typically used to help tools do some reflection.
  77. Helios, for example, uses this to decide what icon the class should display."
  78. ^ 'exception'
  79. ! !
  80. !Error class methodsFor: 'instance creation'!
  81. signal
  82. ^ self new signal
  83. !
  84. signal: aString
  85. ^ self new
  86. signal: aString
  87. ! !
  88. Error subclass: #Halt
  89. instanceVariableNames: ''
  90. package: 'Kernel-Exceptions'!
  91. !Halt commentStamp!
  92. I am provided to support `Object>>#halt`.!
  93. !Halt methodsFor: 'accessing'!
  94. messageText
  95. ^ 'Halt encountered'
  96. !
  97. signalerContextFrom: aContext
  98. "specialized version to find the proper context to open the debugger on.
  99. This will find the first context whose method is no longer on `Halt` or
  100. `Halt class` nor is `#halt` method itself."
  101. ^ aContext findContextSuchThat: [ :context |
  102. (context receiver == self
  103. or: [ (context receiver == self class)
  104. or: [ context method selector = #halt ]]) not ]
  105. ! !
  106. Error subclass: #JavaScriptException
  107. instanceVariableNames: 'exception'
  108. package: 'Kernel-Exceptions'!
  109. !JavaScriptException commentStamp!
  110. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  111. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  112. !JavaScriptException methodsFor: 'accessing'!
  113. context: aMethodContext
  114. "Set the context from the outside.
  115. See boot.js `inContext()` exception handling"
  116. <inlineJS: 'self.context = aMethodContext'>
  117. !
  118. exception
  119. ^ exception
  120. !
  121. exception: anException
  122. exception := anException
  123. !
  124. messageText
  125. <inlineJS: 'return "JavaScript exception: " + $self["@exception"].toString()'>
  126. ! !
  127. !JavaScriptException methodsFor: 'error handling'!
  128. wrap
  129. [ self signal ] tryCatch:
  130. [ self shouldBeStubbed ifTrue: [ self context stubToAtMost: 100 ] ]
  131. ! !
  132. !JavaScriptException methodsFor: 'testing'!
  133. shouldBeStubbed
  134. <inlineJS: 'return $self["@exception"] instanceof RangeError'>
  135. ! !
  136. !JavaScriptException class methodsFor: 'instance creation'!
  137. on: anException
  138. ^ self new
  139. exception: anException;
  140. yourself
  141. !
  142. on: anException context: aMethodContext
  143. ^ self new
  144. exception: anException;
  145. context: aMethodContext;
  146. yourself
  147. ! !
  148. Error subclass: #MessageNotUnderstood
  149. instanceVariableNames: 'message receiver'
  150. package: 'Kernel-Exceptions'!
  151. !MessageNotUnderstood commentStamp!
  152. This exception is provided to support `Object>>doesNotUnderstand:`.!
  153. !MessageNotUnderstood methodsFor: 'accessing'!
  154. message
  155. ^ message
  156. !
  157. message: aMessage
  158. message := aMessage
  159. !
  160. messageText
  161. ^ self receiver asString, ' does not understand #', self message selector
  162. !
  163. receiver
  164. ^ receiver
  165. !
  166. receiver: anObject
  167. receiver := anObject
  168. ! !
  169. Error subclass: #NonBooleanReceiver
  170. instanceVariableNames: 'object'
  171. package: 'Kernel-Exceptions'!
  172. !NonBooleanReceiver commentStamp!
  173. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  174. !NonBooleanReceiver methodsFor: 'accessing'!
  175. object
  176. ^ object
  177. !
  178. object: anObject
  179. object := anObject
  180. ! !
  181. !NonBooleanReceiver class methodsFor: 'instance creation'!
  182. signalOn: anObject
  183. ^ self new
  184. object: anObject;
  185. signal
  186. ! !