Kernel-Exceptions.st 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 class methodsFor: 'instance creation'!
  128. on: anException
  129. ^ self new
  130. exception: anException;
  131. yourself
  132. !
  133. on: anException context: aMethodContext
  134. ^ self new
  135. exception: anException;
  136. context: aMethodContext;
  137. yourself
  138. ! !
  139. Error subclass: #MessageNotUnderstood
  140. instanceVariableNames: 'message receiver'
  141. package: 'Kernel-Exceptions'!
  142. !MessageNotUnderstood commentStamp!
  143. This exception is provided to support `Object>>doesNotUnderstand:`.!
  144. !MessageNotUnderstood methodsFor: 'accessing'!
  145. message
  146. ^ message
  147. !
  148. message: aMessage
  149. message := aMessage
  150. !
  151. messageText
  152. ^ self receiver asString, ' does not understand #', self message selector
  153. !
  154. receiver
  155. ^ receiver
  156. !
  157. receiver: anObject
  158. receiver := anObject
  159. ! !
  160. Error subclass: #NonBooleanReceiver
  161. instanceVariableNames: 'object'
  162. package: 'Kernel-Exceptions'!
  163. !NonBooleanReceiver commentStamp!
  164. NonBooleanReceiver exceptions may be thrown when executing inlined methods such as `#ifTrue:` with a non boolean receiver.!
  165. !NonBooleanReceiver methodsFor: 'accessing'!
  166. object
  167. ^ object
  168. !
  169. object: anObject
  170. object := anObject
  171. ! !