2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//
|
|
|
|
// TODO: is it better to inherit from Object???
|
|
|
|
// or treat Exception specially like UndefinedObject or Class???
|
|
|
|
//
|
2017-01-06 09:53:40 +00:00
|
|
|
class Exception(Apex)
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-11-21 15:05:12 +00:00
|
|
|
var signalContext, handlerContext.
|
|
|
|
var(#get) messageText.
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/*
|
2017-05-04 05:22:45 +00:00
|
|
|
TODO: can i convert 'thisProcess primError' to a relevant exception?
|
|
|
|
var(#class) primExceptTable.
|
|
|
|
|
|
|
|
method(#class) forPrimitiveError: no
|
|
|
|
{
|
|
|
|
^self.primExceptTable at: no
|
|
|
|
}
|
2019-06-19 12:38:09 +00:00
|
|
|
*/
|
2017-05-04 05:22:45 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method(#class) signal
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^self new signal
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method(#class) signal: text
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^self new signal: text
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method handlerContext: context
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
self.handlerContext := context.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method asString
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
2019-06-27 08:06:33 +00:00
|
|
|
^(self class name) & " - " & self.messageText.
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|
2019-07-10 09:19:38 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method signal
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-04-26 15:31:07 +00:00
|
|
|
| exctx exblk retval actpos ctx |
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
self.signalContext := thisContext.
|
2017-01-05 10:16:04 +00:00
|
|
|
exctx := (thisContext sender) findExceptionContext.
|
2017-11-21 09:15:22 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//[exctx notNil] whileTrue: [
|
2017-04-26 15:31:07 +00:00
|
|
|
while (exctx notNil)
|
|
|
|
{
|
2019-07-04 10:04:59 +00:00
|
|
|
exblk := exctx findExceptionHandler: (self class).
|
2019-08-17 15:56:47 +00:00
|
|
|
//if (exblk notNil and: [actpos := exctx basicLastIndex. exctx basicAt: actpos])
|
|
|
|
if ((exblk notNil) and (exctx basicAt: (actpos := exctx basicLastIndex)))
|
2017-04-26 15:31:07 +00:00
|
|
|
{
|
2017-01-05 10:16:04 +00:00
|
|
|
self.handlerContext := exctx.
|
|
|
|
exctx basicAt: actpos put: false.
|
2017-04-26 15:31:07 +00:00
|
|
|
[ retval := exblk value: self ] ensure: [ exctx basicAt: actpos put: true ].
|
2017-01-05 10:16:04 +00:00
|
|
|
thisContext unwindTo: (exctx sender) return: nil.
|
2017-11-21 09:15:22 +00:00
|
|
|
System return: retval to: (exctx sender).
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2017-01-05 10:16:04 +00:00
|
|
|
exctx := (exctx sender) findExceptionContext.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// FATAL ERROR - no exception handler.
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
//thisContext unwindTo: nil return: nil.
|
|
|
|
//thisContext unwindTo: (Processor activeProcess initialContext) return: nil.
|
2019-07-10 09:19:38 +00:00
|
|
|
|
2019-07-12 07:24:37 +00:00
|
|
|
System backtrace.
|
2017-04-26 15:31:07 +00:00
|
|
|
|
2016-07-05 15:22:29 +00:00
|
|
|
thisContext unwindTo: (thisProcess initialContext) return: nil.
|
2019-06-27 08:06:33 +00:00
|
|
|
("### EXCEPTION NOT HANDLED(Exception) #### " & self class name & " - " & self messageText) dump.
|
2019-06-27 06:29:09 +00:00
|
|
|
// TODO: debug the current process???? "
|
2016-07-05 15:22:29 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//Processor activeProcess terminate.
|
2016-07-05 15:22:29 +00:00
|
|
|
thisProcess terminate.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method signal: text
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
self.messageText := text.
|
|
|
|
^self signal.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method pass
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// pass the exception to the outer context
|
2016-06-26 15:03:12 +00:00
|
|
|
((self.handlerContext sender) findExceptionContext) handleException: self.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method return: value
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-11-21 09:15:22 +00:00
|
|
|
if (self.handlerContext notNil) { System return: value to: self.handlerContext }
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method retry
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// TODO: verify if return:to: causes unnecessary stack growth.
|
2017-04-26 15:31:07 +00:00
|
|
|
if (self.handlerContext notNil)
|
|
|
|
{
|
2016-06-26 15:03:12 +00:00
|
|
|
self.handlerContext pc: 0.
|
2017-11-21 09:15:22 +00:00
|
|
|
System return: self to: self.handlerContext.
|
2017-04-26 15:31:07 +00:00
|
|
|
}
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method resume: value
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// TODO: verify if return:to: causes unnecessary stack growth.
|
|
|
|
// is this correct???
|
2017-04-26 15:31:07 +00:00
|
|
|
| ctx |
|
2018-05-30 15:32:09 +00:00
|
|
|
if ((self.signalContext notNil) and (self.handlerContext notNil))
|
2017-04-26 15:31:07 +00:00
|
|
|
{
|
2016-06-26 15:03:12 +00:00
|
|
|
ctx := self.signalContext sender.
|
|
|
|
self.signalContext := nil.
|
|
|
|
self.handlerContext := nil.
|
2017-11-21 09:15:22 +00:00
|
|
|
System return: value to: ctx.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method resume
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^self resume: nil.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//============================================================================
|
2017-01-06 09:53:40 +00:00
|
|
|
extend Context
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method isExceptionContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method isEnsureContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^false
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method ensureBlock
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
^nil
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method findExceptionContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
| ctx |
|
|
|
|
ctx := self.
|
2017-04-26 15:31:07 +00:00
|
|
|
while (ctx notNil)
|
|
|
|
{
|
|
|
|
if (ctx isExceptionContext) { ^ctx }.
|
2016-06-26 15:03:12 +00:00
|
|
|
ctx := ctx sender.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
^nil
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method unwindTo: context return: retval
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// <<private>>
|
|
|
|
// private: called by VM upon unwinding as well as by
|
2019-08-16 15:29:36 +00:00
|
|
|
// Exception>>signal, Error>>signal, Process>>terminate
|
2019-06-27 06:29:09 +00:00
|
|
|
// -------------------------------------------------------------------
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
| ctx stop eb pending_pos |
|
2016-07-04 15:36:10 +00:00
|
|
|
|
2016-06-26 15:03:12 +00:00
|
|
|
ctx := self.
|
|
|
|
stop := false.
|
2017-04-26 15:31:07 +00:00
|
|
|
until (stop)
|
|
|
|
{
|
2016-06-26 15:03:12 +00:00
|
|
|
eb := ctx ensureBlock.
|
2017-04-26 15:31:07 +00:00
|
|
|
if (eb notNil)
|
|
|
|
{
|
2019-06-19 12:38:09 +00:00
|
|
|
/* position of the temporary variable in the ensureBlock that indicates
|
2019-08-17 05:28:25 +00:00
|
|
|
* if the block has been evaluated. see the method BlockContext>>ensure:.
|
|
|
|
* it is the position of the last temporary variable of the method */
|
2019-08-17 15:56:47 +00:00
|
|
|
pending_pos := ctx basicLastIndex.
|
2019-08-17 06:30:34 +00:00
|
|
|
/*
|
2017-04-26 15:31:07 +00:00
|
|
|
if (ctx basicAt: pending_pos)
|
|
|
|
{
|
|
|
|
ctx basicAt: pending_pos put: false.
|
2016-06-26 15:03:12 +00:00
|
|
|
eb value.
|
2019-08-17 06:30:34 +00:00
|
|
|
}*/
|
2019-08-18 17:46:40 +00:00
|
|
|
if (ctx basicAt: pending_pos test: true put: false)
|
|
|
|
{
|
|
|
|
// TODO: what is the best way to handle an exception raised in an ensure block?
|
|
|
|
[ eb value ] on: Exception do: [:ex | System logNl: "WARNING: unhandled exception in ensure block - " & ex messageText ].
|
|
|
|
}.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
stop := (ctx == context).
|
|
|
|
ctx := ctx sender.
|
2016-07-01 16:31:47 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// stop ifFalse: [ stop := ctx isNil ].
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
^retval
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//============================================================================
|
2017-11-21 09:15:22 +00:00
|
|
|
pooldic MethodContext.Preamble
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// this must follow MOO_METHOD_PREAMBLE_EXCEPTION in moo.h
|
2019-09-30 08:46:50 +00:00
|
|
|
EXCEPTION := 13,
|
2017-11-21 09:15:22 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// this must follow MOO_METHOD_PREAMBLE_ENSURE in moo.h
|
2019-09-30 08:46:50 +00:00
|
|
|
ENSURE := 14
|
2017-11-21 09:15:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pooldic MethodContext.Index
|
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// [ value-block ] ensure: [ ensure-block ]
|
|
|
|
// assuming ensure block is a parameter the ensure: method to a
|
|
|
|
// block context, the first parameter is placed after the fixed
|
|
|
|
// instance variables of the method context. As MethodContex has
|
|
|
|
// instance variables, the ensure block must be at the 9th position
|
|
|
|
// which translates to index 8
|
2019-09-30 08:46:50 +00:00
|
|
|
ENSURE := 8,
|
2017-11-21 09:15:22 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// [ ... ] on: Exception: do: [:ex | ... ]
|
2019-09-30 08:46:50 +00:00
|
|
|
FIRST_ON := 8
|
2017-11-21 09:15:22 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
extend MethodContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method isExceptionContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-11-21 09:15:22 +00:00
|
|
|
^self.method preambleCode == MethodContext.Preamble.EXCEPTION.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method isEnsureContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-11-21 09:15:22 +00:00
|
|
|
^self.method preambleCode == MethodContext.Preamble.ENSURE.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method ensureBlock
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-11-21 09:15:22 +00:00
|
|
|
^if (self.method preambleCode == MethodContext.Preamble.ENSURE) { self basicAt: MethodContext.Index.ENSURE } else { nil }
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-07-04 10:04:59 +00:00
|
|
|
method findExceptionHandler: exception_class
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-19 12:38:09 +00:00
|
|
|
/* find an exception handler block for a given exception class.
|
2017-01-05 10:16:04 +00:00
|
|
|
*
|
|
|
|
* for this to work, self must be an exception handler context.
|
|
|
|
* For a single on:do: call,
|
|
|
|
* self class specNumInstVars must return 8.(i.e.MethodContext has 8 instance variables.)
|
|
|
|
* basicAt: 8 must be the on: argument.
|
2019-07-05 08:12:42 +00:00
|
|
|
* basicAt: 9 must be the do: argument
|
|
|
|
*/
|
2017-11-21 09:15:22 +00:00
|
|
|
| size exc i |
|
2019-07-04 10:04:59 +00:00
|
|
|
|
|
|
|
<primitive: #MethodContext_findExceptionHandler:>
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
if (self isExceptionContext)
|
|
|
|
{
|
2019-08-07 05:33:35 +00:00
|
|
|
/* [NOTE] the following loop scans all parameters to the on:do: method.
|
2017-01-05 10:16:04 +00:00
|
|
|
* if the on:do: method contains local temporary variables,
|
2019-07-05 08:12:42 +00:00
|
|
|
* you must change this function to skip scanning local variables.
|
|
|
|
* the current on:do: method has 1 local variable declared.
|
|
|
|
* as local variables are placed after method arguments and
|
|
|
|
* the loop increments 'i' by 2, the last element is naturally
|
|
|
|
* get excluded from inspection.
|
|
|
|
*/
|
2017-01-05 10:16:04 +00:00
|
|
|
size := self basicSize.
|
2019-07-04 10:04:59 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
// start scanning from the position of the first parameter
|
2017-11-21 15:05:12 +00:00
|
|
|
i := MethodContext.Index.FIRST_ON.
|
2017-11-21 09:15:22 +00:00
|
|
|
while (i < size)
|
|
|
|
{
|
2016-06-26 15:03:12 +00:00
|
|
|
exc := self basicAt: i.
|
2018-05-30 15:32:09 +00:00
|
|
|
if ((exception_class == exc) or (exception_class inheritsFrom: exc))
|
2017-11-21 09:15:22 +00:00
|
|
|
{
|
|
|
|
^self basicAt: (i + 1).
|
|
|
|
}.
|
|
|
|
i := i + 2.
|
|
|
|
}.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
^nil.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method handleException: exception
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-19 12:38:09 +00:00
|
|
|
/* -----------------------------------------------------------------
|
2017-01-05 10:16:04 +00:00
|
|
|
* <<private>>
|
|
|
|
* called by Exception>>signal.
|
|
|
|
* this method only exists in the MethodContext and UndefinedObject.
|
|
|
|
* the caller must make sure that the receiver object is
|
|
|
|
* a method context or nil. Exception>>signal invokes this method
|
|
|
|
* only for an exception context which is a method context. it
|
|
|
|
* invokes it for nil when no exception context is found.
|
2019-06-19 12:38:09 +00:00
|
|
|
* ---------------------------------------------------------------- */
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
| excblk retval actpos |
|
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* position of the temporary variable 'exception_active' in MethodContext>>on:do.
|
|
|
|
* for this code to work, it must be the last temporary variable in the method. */
|
2019-08-17 15:56:47 +00:00
|
|
|
//actpos := (self basicSize) - 1.
|
|
|
|
actpos := self basicLastIndex.
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2019-07-04 10:04:59 +00:00
|
|
|
excblk := self findExceptionHandler: (exception class).
|
2018-05-30 15:32:09 +00:00
|
|
|
if ((excblk isNil) or ((self basicAt: actpos) not))
|
2017-04-26 15:31:07 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// self is an exception context but doesn't have a matching
|
|
|
|
// exception handler or the exception context is already
|
|
|
|
// in the middle of evaluation.
|
2016-06-26 15:03:12 +00:00
|
|
|
^(self.sender findExceptionContext) handleException: exception.
|
2017-04-26 15:31:07 +00:00
|
|
|
}.
|
2016-06-26 15:03:12 +00:00
|
|
|
|
|
|
|
exception handlerContext: self.
|
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* -----------------------------------------------------------------
|
2017-01-05 10:16:04 +00:00
|
|
|
* if an exception occurs within an exception handler block,
|
|
|
|
* the search will reach this context again as the exception block
|
|
|
|
* is evaluated before actual unwinding. set the temporary variable
|
|
|
|
* in the exception context to mask out this context from the search
|
|
|
|
* list.
|
2019-06-19 12:38:09 +00:00
|
|
|
* ---------------------------------------------------------------- */
|
2016-06-26 15:03:12 +00:00
|
|
|
self basicAt: actpos put: false.
|
|
|
|
[ retval := excblk value: exception ] ensure: [
|
|
|
|
self basicAt: actpos put: true
|
|
|
|
].
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//(self.sender isNil) ifTrue: [ "TODO: CANNOT RETURN" ].
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* -----------------------------------------------------------------
|
2017-01-05 10:16:04 +00:00
|
|
|
* return to self.sender which is a caller of the exception context (on:do:)
|
|
|
|
* pass the first ensure context between thisContext and self.sender.
|
|
|
|
* [ [Exception signal: 'xxx'] ensure: [20] ] on: Exception do: [:ex | ...]
|
2019-06-19 12:38:09 +00:00
|
|
|
* ---------------------------------------------------------------- */
|
2016-06-26 15:03:12 +00:00
|
|
|
thisContext unwindTo: self.sender return: nil.
|
2017-11-21 09:15:22 +00:00
|
|
|
System return: retval to: self.sender.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//============================================================================
|
2017-01-06 09:53:40 +00:00
|
|
|
extend BlockContext
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
method on: anException do: anExceptionBlock
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* -------------------------------
|
2017-01-05 10:16:04 +00:00
|
|
|
thisContext isExceptionContext dump.
|
2016-06-26 15:03:12 +00:00
|
|
|
(thisContext basicSize) dump.
|
2019-06-27 06:29:09 +00:00
|
|
|
(thisContext basicAt: 8) dump. // this should be anException
|
|
|
|
(thisContext basicAt: 9) dump. // this should be anExceptionBlock
|
|
|
|
(thisContext basicAt: 10) dump. // this should be handlerActive
|
2017-01-05 10:16:04 +00:00
|
|
|
'on:do: ABOUT TO EVALUE THE RECEIVER BLOCK' dump.
|
2019-06-19 12:38:09 +00:00
|
|
|
---------------------------------- */
|
2016-06-26 15:03:12 +00:00
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method on: exc1 do: blk1 on: exc2 do: blk2
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
2017-11-21 15:05:12 +00:00
|
|
|
method on: exc1 do: blk1 on: exc2 do: blk2 on: exc3 do: blk3
|
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
2019-07-05 08:12:42 +00:00
|
|
|
|
|
|
|
method on: exc1 do: blk1 on: exc2 do: blk2 on: exc3 do: blk3 on: exc4 do: blk4
|
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
|
|
|
method on: exc1 do: blk1 on: exc2 do: blk2 on: exc3 do: blk3 on: exc4 do: blk4 on: exc5 do: blk5
|
|
|
|
{
|
|
|
|
| exception_active |
|
|
|
|
<exception>
|
|
|
|
exception_active := true.
|
|
|
|
^self value.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method ensure: aBlock
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-04-26 15:31:07 +00:00
|
|
|
| retval pending |
|
2016-06-26 15:03:12 +00:00
|
|
|
<ensure>
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
pending := true.
|
2016-06-26 15:03:12 +00:00
|
|
|
retval := self value.
|
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* the temporary variable 'pending' may get changed
|
2017-04-26 15:31:07 +00:00
|
|
|
* during evaluation for exception handling.
|
2019-06-19 12:38:09 +00:00
|
|
|
* it gets chagned in Context>>unwindTo:return: */
|
2019-08-17 06:30:34 +00:00
|
|
|
/*if (pending) { pending := false. aBlock value }.*/
|
2019-08-17 15:56:47 +00:00
|
|
|
if (thisContext basicAt: (thisContext basicLastIndex) test: true put: false) { aBlock value }.
|
2016-06-26 15:03:12 +00:00
|
|
|
^retval
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method ifCurtailed: aBlock
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-04-26 15:31:07 +00:00
|
|
|
| v pending |
|
|
|
|
pending := true.
|
|
|
|
[ v := self value. pending := false. ] ensure: [ if (pending) { aBlock value } ].
|
2016-06-26 15:03:12 +00:00
|
|
|
^v.
|
|
|
|
}
|
2017-11-21 15:05:12 +00:00
|
|
|
|
|
|
|
method catch
|
|
|
|
{
|
|
|
|
^self on: Exception do: [:ex | ex ]
|
|
|
|
on: Error do: [:err | err ].
|
|
|
|
}
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//============================================================================
|
2017-01-09 09:11:36 +00:00
|
|
|
class PrimitiveFailureException(Exception)
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
2017-05-11 14:59:20 +00:00
|
|
|
var errcode.
|
|
|
|
|
|
|
|
method(#class) withErrorCode: code
|
|
|
|
{
|
|
|
|
^(self new) errorCode: code; yourself.
|
|
|
|
}
|
|
|
|
|
|
|
|
method signal
|
|
|
|
{
|
|
|
|
self.errcode := thisProcess primError.
|
|
|
|
^super signal
|
|
|
|
}
|
|
|
|
|
|
|
|
method errorCode
|
|
|
|
{
|
|
|
|
^self.errcode
|
|
|
|
}
|
|
|
|
|
|
|
|
method errorCode: code
|
|
|
|
{
|
|
|
|
self.errcode := code
|
|
|
|
}
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 09:11:36 +00:00
|
|
|
class NoSuchMessageException(Exception)
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class IndexOutOfRangeException(Exception)
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
class SubclassResponsibilityException(Exception)
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:11:36 +00:00
|
|
|
class NotImplementedException(Exception)
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-09 09:11:36 +00:00
|
|
|
class InvalidArgumentException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:46:44 +00:00
|
|
|
class ProhibitedMessageException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-20 18:01:04 +00:00
|
|
|
class NotFoundException(Exception)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class KeyNotFoundException(NotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class ValueNotFoundException(NotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
class IndexNotFoundException(NotFoundException)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
extend Apex
|
|
|
|
{
|
2019-10-24 15:17:46 +00:00
|
|
|
method(#dual) error: text
|
|
|
|
{
|
|
|
|
Exception signal: text.
|
|
|
|
}
|
|
|
|
|
2017-05-09 15:48:44 +00:00
|
|
|
method(#dual,#liberal) primitiveFailed(method)
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2017-05-11 14:59:20 +00:00
|
|
|
| a b msg ec ex |
|
2017-05-09 15:48:44 +00:00
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/* since method is an argument, the caller can call this method
|
2017-05-16 02:04:18 +00:00
|
|
|
* from a context chain where the method context actually doesn't exist.
|
|
|
|
* when a primitive method is defined using the #primitive method,
|
|
|
|
* the VM invokes this primtiveFailed method without creating
|
|
|
|
* the context for the primitive method.
|
|
|
|
* method(#primitive) xxx.
|
|
|
|
* method(#primitive) xxx { <primitive: #_xxx> ^self primitiveFailed(thisContext method). }
|
|
|
|
* in the former definition, primitiveFailed is called without
|
|
|
|
* an activate context for the method xxx if the primitive fails.
|
|
|
|
* on the other handle, in the latter definition, the context
|
|
|
|
* for the method is activated first before primitiveFailed is
|
|
|
|
* invoked. in the context chain, the method for xxx is found.
|
2019-06-19 12:38:09 +00:00
|
|
|
*/
|
2017-05-16 02:04:18 +00:00
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
/*System logNl: 'Arguments: '.
|
2017-05-09 15:48:44 +00:00
|
|
|
a := 0.
|
2017-05-08 16:00:55 +00:00
|
|
|
b := thisContext vargCount.
|
|
|
|
while (a < b)
|
|
|
|
{
|
2017-05-09 15:48:44 +00:00
|
|
|
System logNl: (thisContext vargAt: a) asString.
|
2017-05-08 16:00:55 +00:00
|
|
|
a := a + 1.
|
2019-06-19 12:38:09 +00:00
|
|
|
}.*/
|
2017-05-16 02:04:18 +00:00
|
|
|
|
2017-05-11 14:59:20 +00:00
|
|
|
ec := thisProcess primError.
|
2017-05-16 02:04:18 +00:00
|
|
|
msg := thisProcess primErrorMessage.
|
|
|
|
if (msg isNil) { msg := ec asString }.
|
2019-06-27 08:06:33 +00:00
|
|
|
if (method notNil) { msg := msg & " - " & (method owner name) & ">>" & (method name) }.
|
2017-05-16 02:04:18 +00:00
|
|
|
|
2019-06-27 06:29:09 +00:00
|
|
|
//# TODO: convert an exception to a more specific one depending on the error code.
|
|
|
|
//#if (ec == Error.Code.ERANGE) { self index: index outOfRange: (self basicSize) }
|
2019-07-26 04:40:33 +00:00
|
|
|
//# elif (ec == Error.Code.EPERM) { self messageProhibited: method name }
|
|
|
|
//# elif (ec == Error.Code.ENOIMPL) { self subclassResponsibility: method name }.
|
2017-12-03 17:08:04 +00:00
|
|
|
|
2019-06-19 12:38:09 +00:00
|
|
|
(PrimitiveFailureException /* in: method */ withErrorCode: ec) signal: msg.
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
method(#dual) doesNotUnderstand: message_name
|
2016-06-26 15:03:12 +00:00
|
|
|
{
|
2019-06-27 06:29:09 +00:00
|
|
|
// TODO: implement this properly
|
2018-05-02 09:53:02 +00:00
|
|
|
| class_name ctx |
|
2017-04-26 15:31:07 +00:00
|
|
|
class_name := if (self class == Class) { self name } else { self class name }.
|
2018-05-02 09:53:02 +00:00
|
|
|
|
2019-07-12 07:24:37 +00:00
|
|
|
System backtrace.
|
2018-05-02 09:53:02 +00:00
|
|
|
|
2019-06-27 08:06:33 +00:00
|
|
|
NoSuchMessageException signal: (message_name & " not understood by " & class_name).
|
2016-06-26 15:03:12 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
method(#dual) index: index outOfRange: ubound
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
2019-06-27 08:06:33 +00:00
|
|
|
IndexOutOfRangeException signal: "Out of range".
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|
2016-06-26 15:03:12 +00:00
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
method(#dual) subclassResponsibility: method_name
|
2017-01-05 10:16:04 +00:00
|
|
|
{
|
2019-06-27 08:06:33 +00:00
|
|
|
SubclassResponsibilityException signal: ("Subclass must implement " & method_name).
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 18:01:04 +00:00
|
|
|
method(#dual) shouldNotImplement: method_name
|
|
|
|
{
|
2019-06-27 08:06:33 +00:00
|
|
|
SubclassResponsibilityException signal: ("Message should not be implemented - " & method_name).
|
2018-06-20 18:01:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
method(#dual) notImplemented: method_name
|
2017-01-09 09:11:36 +00:00
|
|
|
{
|
2017-04-26 15:31:07 +00:00
|
|
|
| class_name |
|
|
|
|
class_name := if (self class == Class) { self name } else { self class name }.
|
2019-06-27 08:06:33 +00:00
|
|
|
NotImplementedException signal: (method_name & " not implemented by " & class_name).
|
2017-01-09 09:11:36 +00:00
|
|
|
}
|
|
|
|
|
2017-04-26 15:31:07 +00:00
|
|
|
method(#dual) messageProhibited: method_name
|
2017-04-19 16:46:44 +00:00
|
|
|
{
|
2017-04-26 15:31:07 +00:00
|
|
|
| class_name |
|
|
|
|
class_name := if (self class == Class) { self name } else { self class name }.
|
2019-06-27 08:06:33 +00:00
|
|
|
ProhibitedMessageException signal: (method_name & " not allowed for " & class_name).
|
2017-04-19 16:46:44 +00:00
|
|
|
}
|
2017-01-05 10:16:04 +00:00
|
|
|
}
|