Error and Notification [ doSomething ] on: ExceptionClass do: [ :ex | something ]anException signaldefaultAction is executed when an exception occurs and it is not trappedConvenient messages:
ensure:, ifCurtailed: [ do something ] on: ExceptionClass do: [ :ex | something ]Example:
| x y |
x := 7.
y := 0.
[ x / y ]
on: ZeroDivide
do: [ :exception | Transcript show: exception description; cr.
0 ]
> 0To raise an exception:
signal or signal: aMessage (AuthorNameRequest new initialAnswer: 'Stef') signal
(Warning new messageText: 'Pay attention') signalUsually classes propose a shortcut
OutOfMemory signal.
Warning signal: 'description of the exception'
SUnit offers should:raise: and shouldnt:raise: to check occurrence of exceptions.
testNameOfMonth
self assert: (Date nameOfMonth: 1) equals: #January.
self
shouldnt: [ Date nameOfMonth: 2 ]
raise: SubscriptOutOfBounds.
self
should: [ Date nameOfMonth: 13 ]
raise: SubscriptOutOfBounds.Error: all errors (subscript, message not understood, division by zeroHalt: to stop the execution (and get a debugger)Notification: non fatal exceptions (deprecation, warning, timedout)UnhandledError: when an error occurs and that it is not trapped
When you send an unknown message Point new strangeAndBizarre
ProtoObject >> doesNotUnderstand: aMessage
^ MessageNotUnderstood new
message: aMessage;
receiver: self;
signalTo support API migration, Pharo uses deprecation When the deprecation setting is on, a warning is raised when a deprecated method is executed
MenuItem >> title: aString
"Add a title line at the top of this menu."
self deprecated: 'Use method addTitle: instead' on: '29 september' in: #Pharo40.
self addTitle: aString
Create an instance of Deprecation and signal it
deprecated: anExplanationString on: date in: version
"Warn that the sending method has been deprecated"
(Deprecation
method: thisContext sender method
explanation: anExplanationString
on: date
in: version) signal [ do some work ]
on: ZeroDivide, Warning
do: [ :ex | what you want ]Or
| exceptionSets |
exceptionSets := ExceptionSet with: ZeroDivide with: Warning.
[do some work]
on: exceptionSets
do: [ :ex | what you want ][ doSomething ] ensure: [ alwaysExecuteThis ]spyOn: aBlock
"Profile system activity during execution of aBlock."
self startProfiling.
aBlock ensure: [ self stopProfiling ][ doSomething ] ifCurtailed: [ onProblem ]wait
"Schedule this Delay, then wait on its semaphore. The current process will be suspended for the amount of time specified when this Delay was created."
self schedule.
[ delaySemaphore wait ] ifCurtailed: [ self unschedule ] [ aaaa ] on: Error do: [ bbb ] adds Error,bbb to the beginning of the listJust for your information ;)
Within an handler [ aaa ] on: anExceptionClass do: [ anHandler ], we can:
return:)retryUsing:)resume:)pass)resignalAs:)[ Notification signal. 'Value from protected block' ]
on: Notification
do: [ :ex |ex return: 'Value from handler' ]
> 'Value from handler'We return a different string on normal or notification
Warning, Notification and subclasses are resumable
[ Notification signal. 'Value from protected block' ]
on: Notification
do: [ :ex | ex resume: 'Value from handler' ]
> 'Value from protected Block'.Notification signal raises an exceptionresume: restores the context and the value returned normally as if the notification did not occurRaising
anException signalInstalling:
[ doSomething ] on: ExceptionClass do: [ :ex | something ][ doSomething ] ensure: [ alwaysDoThis ] [ doSomething ] ifCurtailed: [ onProblem ]/