Kernel-Exceptions.st 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. Smalltalk createPackage: 'Kernel-Exceptions'!
  2. Object subclass: #Error
  3. slots: {#message. #stack. #amberHandled. #context. #smalltalkError}
  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. amberHandled := true
  14. !
  15. beUnhandled
  16. amberHandled := false
  17. !
  18. context
  19. ^ context
  20. !
  21. context: aMethodContext
  22. context := aMethodContext
  23. !
  24. jsStack
  25. ^ stack
  26. !
  27. messageText
  28. ^ message
  29. !
  30. messageText: aString
  31. message := aString
  32. !
  33. signalerContext
  34. ^ self signalerContextFrom: self context
  35. !
  36. signalerContextFrom: aContext
  37. "Find the first sender of signal(:), the first context which is neither
  38. for an instance method nor for a class side method of Exception (or subclass).
  39. This will make sure that the same context is found for both, `Error signal`
  40. and `Error new signal`"
  41. ^ aContext findContextSuchThat: [ :one |
  42. (one receiver == self
  43. or: [ one receiver == self class ]) not ]
  44. ! !
  45. !Error methodsFor: 'initialization'!
  46. initialize
  47. self messageText: 'Errorclass: ', self class name.
  48. ! !
  49. !Error methodsFor: 'private'!
  50. basicSignal
  51. <inlineJS: 'throw self;'>
  52. ! !
  53. !Error methodsFor: 'signaling'!
  54. outer
  55. "Pharo compatibility. Just sends #pass."
  56. ^ self pass
  57. !
  58. pass
  59. "Let outer handler take care of this."
  60. self beUnhandled; basicSignal
  61. !
  62. resignal
  63. self deprecatedAPI: 'Use #pass.'.
  64. ^ self pass
  65. !
  66. signal
  67. self beUnhandled; context: thisContext; basicSignal
  68. !
  69. signal: aString
  70. self messageText: aString; signal
  71. ! !
  72. !Error methodsFor: 'testing'!
  73. isError
  74. ^ true
  75. !
  76. wasHandled
  77. ^ amberHandled == true
  78. ! !
  79. !Error class methodsFor: 'accessing'!
  80. classTag
  81. "Returns a tag or general category for this class.
  82. Typically used to help tools do some reflection.
  83. Helios, for example, uses this to decide what icon the class should display."
  84. ^ 'exception'
  85. ! !
  86. !Error class methodsFor: 'instance creation'!
  87. messageText: aString
  88. ^ self new
  89. messageText: aString;
  90. yourself
  91. !
  92. signal
  93. ^ self new signal
  94. !
  95. signal: aString
  96. ^ self new
  97. signal: aString
  98. ! !
  99. Error subclass: #Halt
  100. slots: {}
  101. package: 'Kernel-Exceptions'!
  102. !Halt commentStamp!
  103. I am provided to support `Object>>#halt`.!
  104. !Halt methodsFor: 'accessing'!
  105. messageText
  106. ^ 'Halt encountered'
  107. !
  108. signalerContextFrom: aContext
  109. "specialized version to find the proper context to open the debugger on.
  110. This will find the first context whose method is no longer on `Halt` or
  111. `Halt class` nor is `#halt` method itself."
  112. ^ aContext findContextSuchThat: [ :one |
  113. (one receiver == self
  114. or: [ (one receiver == self class)
  115. or: [ one method selector = #halt ]]) not ]
  116. ! !
  117. Error subclass: #JavaScriptException
  118. slots: {#exception}
  119. package: 'Kernel-Exceptions'!
  120. !JavaScriptException commentStamp!
  121. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  122. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  123. !JavaScriptException methodsFor: 'accessing'!
  124. exception
  125. ^ exception
  126. !
  127. exception: anException
  128. exception := anException
  129. !
  130. messageText
  131. <inlineJS: 'return "JavaScript exception: " + $self.exception.toString()'>
  132. ! !
  133. !JavaScriptException class methodsFor: 'instance creation'!
  134. on: anException
  135. ^ self new
  136. exception: anException;
  137. yourself
  138. !
  139. on: anException context: aMethodContext
  140. ^ self new
  141. exception: anException;
  142. context: aMethodContext;
  143. yourself
  144. ! !
  145. Error subclass: #MessageNotUnderstood
  146. slots: {#smalltalkMessage. #receiver}
  147. package: 'Kernel-Exceptions'!
  148. !MessageNotUnderstood commentStamp!
  149. This exception is provided to support `Object>>doesNotUnderstand:`.!
  150. !MessageNotUnderstood methodsFor: 'accessing'!
  151. message
  152. ^ smalltalkMessage
  153. !
  154. message: aMessage
  155. smalltalkMessage := aMessage
  156. !
  157. messageText
  158. ^ self receiver asString, ' does not understand #', self message selector
  159. !
  160. receiver
  161. ^ receiver
  162. !
  163. receiver: anObject
  164. receiver := anObject
  165. ! !
  166. Error subclass: #NonBooleanReceiver
  167. slots: {#object}
  168. package: 'Kernel-Exceptions'!
  169. !NonBooleanReceiver commentStamp!
  170. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  171. !NonBooleanReceiver methodsFor: 'accessing'!
  172. object
  173. ^ object
  174. !
  175. object: anObject
  176. object := anObject
  177. ! !
  178. !NonBooleanReceiver class methodsFor: 'instance creation'!
  179. signalOn: anObject
  180. ^ self new
  181. object: anObject;
  182. signal
  183. ! !
  184. Error subclass: #NonLifoReturn
  185. slots: {#value}
  186. package: 'Kernel-Exceptions'!
  187. !NonLifoReturn methodsFor: 'accessing'!
  188. messageText
  189. ^ 'Non-LIFO return: ', self value asString
  190. !
  191. value
  192. ^ value
  193. !
  194. value: anObject
  195. value := anObject
  196. ! !
  197. !NonLifoReturn class methodsFor: 'instance creation'!
  198. reifyIfFeasible: anObject
  199. "If anObject represents non-local return, reify it as my instance.
  200. Otherwise, return anObject as-is."
  201. <inlineJS: '
  202. return Array.isArray(anObject) && anObject.length === 1 ?
  203. $self._value_(anObject[0]) : anObject
  204. '>
  205. !
  206. value: anObject
  207. ^ super new
  208. value: anObject;
  209. yourself
  210. ! !