Kernel-Exceptions.st 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. signal
  88. ^ self new signal
  89. !
  90. signal: aString
  91. ^ self new
  92. signal: aString
  93. ! !
  94. Error subclass: #Halt
  95. slots: {}
  96. package: 'Kernel-Exceptions'!
  97. !Halt commentStamp!
  98. I am provided to support `Object>>#halt`.!
  99. !Halt methodsFor: 'accessing'!
  100. messageText
  101. ^ 'Halt encountered'
  102. !
  103. signalerContextFrom: aContext
  104. "specialized version to find the proper context to open the debugger on.
  105. This will find the first context whose method is no longer on `Halt` or
  106. `Halt class` nor is `#halt` method itself."
  107. ^ aContext findContextSuchThat: [ :one |
  108. (one receiver == self
  109. or: [ (one receiver == self class)
  110. or: [ one method selector = #halt ]]) not ]
  111. ! !
  112. Error subclass: #JavaScriptException
  113. slots: {#exception}
  114. package: 'Kernel-Exceptions'!
  115. !JavaScriptException commentStamp!
  116. A JavaScriptException is thrown when a non-Smalltalk exception occurs while in the Smalltalk stack.
  117. See `boot.js` `inContext()` and `BlockClosure >> on:do:`!
  118. !JavaScriptException methodsFor: 'accessing'!
  119. exception
  120. ^ exception
  121. !
  122. exception: anException
  123. exception := anException
  124. !
  125. messageText
  126. <inlineJS: 'return "JavaScript exception: " + $self.exception.toString()'>
  127. ! !
  128. !JavaScriptException class methodsFor: 'instance creation'!
  129. on: anException
  130. ^ self new
  131. exception: anException;
  132. yourself
  133. !
  134. on: anException context: aMethodContext
  135. ^ self new
  136. exception: anException;
  137. context: aMethodContext;
  138. yourself
  139. ! !
  140. Error subclass: #MessageNotUnderstood
  141. slots: {#smalltalkMessage. #receiver}
  142. package: 'Kernel-Exceptions'!
  143. !MessageNotUnderstood commentStamp!
  144. This exception is provided to support `Object>>doesNotUnderstand:`.!
  145. !MessageNotUnderstood methodsFor: 'accessing'!
  146. message
  147. ^ smalltalkMessage
  148. !
  149. message: aMessage
  150. smalltalkMessage := aMessage
  151. !
  152. messageText
  153. ^ self receiver asString, ' does not understand #', self message selector
  154. !
  155. receiver
  156. ^ receiver
  157. !
  158. receiver: anObject
  159. receiver := anObject
  160. ! !
  161. Error subclass: #NonBooleanReceiver
  162. slots: {#object}
  163. package: 'Kernel-Exceptions'!
  164. !NonBooleanReceiver commentStamp!
  165. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  166. !NonBooleanReceiver methodsFor: 'accessing'!
  167. object
  168. ^ object
  169. !
  170. object: anObject
  171. object := anObject
  172. ! !
  173. !NonBooleanReceiver class methodsFor: 'instance creation'!
  174. signalOn: anObject
  175. ^ self new
  176. object: anObject;
  177. signal
  178. ! !