SlideShare a Scribd company logo
Stéphane Ducasse
Stéphane Ducasse
stephane.ducasse@inria.fr
http://stephane.ducasse.free.fr/
1
Exceptions...a simple
introduction
S.Ducasse 2
Standardized by ANSI in 1996
Exception is the root of the exception
hierarchy: 84 predefined exceptions. The two
most important classes are:
Error
Notification
Specialised into predefined exceptions ->
subclass them to create your own exceptions
Some methods of Exception:
defaultAction is executed when an exception occurs
description string describing the actual exception
Exceptions
S.Ducasse 3
|x y|
x := 7.
y := 0.
[x/y]
on: ZeroDivide
do: [:exception| Transcript show:
exception description, cr.
0....]
Catching an Exception
S.Ducasse 4
Signaling an Exception
Error signal
Warning signal: ‘description of the exception’
S.Ducasse 5
Exception
an Exception Handler is defined using on:do: and is
composed of
an exception class (ZeroDivide) and
a handler block [:theException| Transcript show: ‘ division
by zero’]
An Exception Handler completes by returning
the value of the handler block in place of the
value of the protected block (here [x/y]).
We can exit the current method by putting an
explicit return inside the handler block
S.Ducasse 6
The Main Exceptions of VW
Exception class Exceptional Event Default Action
Error Any program error Open a Notifier a Notifier
ArithmeticError
Any error evaluating an
arithmetic
Inherited from Error
MessageNotUnderstood
Any unusual event that
does not impair
continued execution of
the program
Inherited from Error
Notification
Notification Any
unusual event that does
not impair continued
execution of the
program Do nothing
continuing executing
Do nothing continuing
executing
An unusual event that Display Yes/No dialog
S.Ducasse 7
Exception Sets
[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]
Exception Sets
S.Ducasse 8
Each process has its own exception environment: an
ordered list of active handlers.
Process starts -> list empty
[aaaa] on: Error do: [bbb] -> Error,bbb added to the
beginning of the list
When an exception is signaled, the system sends a
message to the first handler of the exception handler.
If the handler cannot handle the exception, the next
one is asked
If no handler can handle the exception then the default
action is performed
Exception Environment
8
S.Ducasse 9
A handler block completes by executing the last
statement of the block. The value of the last statement
is then the value returned by the handler block.
Where this value should be returned depends:
Nonresumable (Error)
Sq: ([Error signal. ‘Value from protected
block’]
on: Error
do: [:ex|ex return: ‘Value from handler’])
> ‘Value from handler’
Resumable and Non-Resumable (i)
S.Ducasse 10
Resumable (Warning, Notification)
In this case Notification signal raises an exception,
then the context is restored and the value returned
normally
[Notification raiseSignal. ‘Value from protected block’]
on: Notification
do: [:ex|ex resume: ‘Value from handler’]
[Notification signal. 'Value from protected block']
on: Notification
do: [:ex|ex resume: 'Value from handler']
> ‘Value from protected Block’.
Resumable and Non-Resumable (ii)
S.Ducasse 11
Transcript show:
[Notification raiseSignal. 'Value from protected block']
on: Notification
do: [:ex| Transcript show: 'Entering handler '.
'Value from handler'. '5']
-> Entering handler 5
Resume:/Return:
S.Ducasse 12
Transcript show: [Notification raiseSignal. 'Value from
protected block']
on: Notification
do: [:ex| Transcript show: 'Entering handler '.
ex resume: 'Value from handler'. '5']
> Entering handler Value from protected block
Transcript show: [Notification raiseSignal. 'Value from
protected']
on: Notification
do: [:ex| Transcript show: 'Entering handler '.
ex return: 'Value from handler'. '5']
-> Entering handler Value from handler
Resume:/Return:
S.Ducasse 13
exit or exit: (VW specific) Resumes on a resumable
and returns on a nonresumable exception
resume or resume: Attempts to continue processing
the protected block, immeditely following the message
that triggered the exception.
return or return: ends processing the protected block
that triggered the exception
retry re-evaluates the protected block
retryUsing: evaluates a new block in place of the
protected block
Exiting Handlers Explicitly
S.Ducasse 14
resignalAs: resignal the exception as another on
pass exit the current handler and pass to the next
outer handler, control does not return to the passer
outer as with pass, except will regain control if the
outer handler resumes
exit:, resume: and return: return their argument as the
return value, instead of the value of the final statement
of the handler block
Exiting Handlers Explicitly (ii)
S.Ducasse 15
Look in Exception class examples categories
-2.0 to: 2.0 do: [ :i |
[ 10.0 / i. Transcript cr; show: i printString ]
on: Number divisionByZeroSignal do:
[:ex | Transcript cr; show: 'divideByZero abort'.
ex return ]
]
-2.0
-1.0
divideByZero abort
1.0
2.0
Examples
S.Ducasse 16
retry recreates the exception environment of active
handlers
[ x /y]
on: ZeroDivide
do: [:exception|
y := 0.00001.
exception retry]
Examples

More Related Content

PPT
Sequences
PPT
Menu Driven programs in Java
DOCX
Conditional Control
PPT
C++loop statements
DOCX
Programming Fundamentals lecture 8
PDF
C programming
PDF
Generators
PPTX
Lecture 5
Sequences
Menu Driven programs in Java
Conditional Control
C++loop statements
Programming Fundamentals lecture 8
C programming
Generators
Lecture 5

What's hot (10)

PPT
Os3 2
ODT
Exp 8...
PDF
Func up your code
PPTX
Triggers
DOCX
Arduino Code
DOCX
Generator iterator methods
PDF
5 c control statements looping
PPTX
Formalization Machines and Sastes
PPT
Handling
Os3 2
Exp 8...
Func up your code
Triggers
Arduino Code
Generator iterator methods
5 c control statements looping
Formalization Machines and Sastes
Handling
Ad

Viewers also liked (20)

PPT
Stoop sed-class initialization
PPT
Stoop 305-reflective programming5
PPT
12 virtualmachine
PPT
Stoop 414-smalltalk elementsofdesign
PPT
Stoop ed-some principles
PPT
PPT
Stoop ed-frameworks
PPT
Stoop 421-design heuristics
PPT
Stoop ed-inheritance composition
PPT
10 - OOP - Inheritance (a)
PPT
Stoop 440-adaptor
PPT
Stoop ed-subtyping subclassing
PPT
10 reflection
PPT
Stoop 303-advanced blocks
PPT
Stoop 431-visitor
PPT
Stoop 437-proxy
PPT
Double Dispatch
PPT
10 - OOP - Inheritance (b)
PPT
7 - OOP - OO Concepts
PPT
Stoop sed-class initialization
Stoop 305-reflective programming5
12 virtualmachine
Stoop 414-smalltalk elementsofdesign
Stoop ed-some principles
Stoop ed-frameworks
Stoop 421-design heuristics
Stoop ed-inheritance composition
10 - OOP - Inheritance (a)
Stoop 440-adaptor
Stoop ed-subtyping subclassing
10 reflection
Stoop 303-advanced blocks
Stoop 431-visitor
Stoop 437-proxy
Double Dispatch
10 - OOP - Inheritance (b)
7 - OOP - OO Concepts
Ad

Similar to 14 - Exceptions (20)

PPTX
Exception handling
PPT
Exception handling
PPT
Exception handling
PPTX
Exception handling
PPTX
Exception handling
PDF
Exception handling
PPTX
Chapter 5
PPTX
Exceptions
PPTX
Exceptions
PPT
Unit 5 Java
PPT
Exception handling and templates
PPT
Exception Handling Mechanism in .NET CLR
PDF
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
PPT
JP ASSIGNMENT SERIES PPT.ppt
PPTX
exception%20handlingcpp.pptx
PPT
Handling Exceptions In C & C++[Part A]
PPT
Exception Handling in java masters of computer application
PPTX
7.error management and exception handling
DOCX
Unit5 java
PPTX
Exception handling, Stream Classes, Multithread Programming
Exception handling
Exception handling
Exception handling
Exception handling
Exception handling
Exception handling
Chapter 5
Exceptions
Exceptions
Unit 5 Java
Exception handling and templates
Exception Handling Mechanism in .NET CLR
CS3391 -OOP -UNIT – III NOTES FINAL.pdf
JP ASSIGNMENT SERIES PPT.ppt
exception%20handlingcpp.pptx
Handling Exceptions In C & C++[Part A]
Exception Handling in java masters of computer application
7.error management and exception handling
Unit5 java
Exception handling, Stream Classes, Multithread Programming

More from The World of Smalltalk (20)

PDF
05 seaside canvas
PPT
PPT
PPT
09 metaclasses
PPT
08 refactoring
PPT
07 bestpractice
PPT
PPT
03 standardclasses
PPT
Stoop sed-smells
PPT
Stoop sed-sharing ornot
PPT
Stoop sed-class initialization
PPT
Stoop metaclasses
PPT
Stoop ed-unit ofreuse
PPT
PPT
Stoop ed-dual interface
PPT
Stoop ed-class forreuse
05 seaside canvas
09 metaclasses
08 refactoring
07 bestpractice
03 standardclasses
Stoop sed-smells
Stoop sed-sharing ornot
Stoop sed-class initialization
Stoop metaclasses
Stoop ed-unit ofreuse
Stoop ed-dual interface
Stoop ed-class forreuse

Recently uploaded (20)

PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Classroom Observation Tools for Teachers
PPTX
Presentation on HIE in infants and its manifestations
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
master seminar digital applications in india
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Computing-Curriculum for Schools in Ghana
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
Presentation on HIE in infants and its manifestations
STATICS OF THE RIGID BODIES Hibbelers.pdf
A systematic review of self-coping strategies used by university students to ...
Supply Chain Operations Speaking Notes -ICLT Program
01-Introduction-to-Information-Management.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Anesthesia in Laparoscopic Surgery in India
master seminar digital applications in india
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Computing-Curriculum for Schools in Ghana

14 - Exceptions

  • 2. S.Ducasse 2 Standardized by ANSI in 1996 Exception is the root of the exception hierarchy: 84 predefined exceptions. The two most important classes are: Error Notification Specialised into predefined exceptions -> subclass them to create your own exceptions Some methods of Exception: defaultAction is executed when an exception occurs description string describing the actual exception Exceptions
  • 3. S.Ducasse 3 |x y| x := 7. y := 0. [x/y] on: ZeroDivide do: [:exception| Transcript show: exception description, cr. 0....] Catching an Exception
  • 4. S.Ducasse 4 Signaling an Exception Error signal Warning signal: ‘description of the exception’
  • 5. S.Ducasse 5 Exception an Exception Handler is defined using on:do: and is composed of an exception class (ZeroDivide) and a handler block [:theException| Transcript show: ‘ division by zero’] An Exception Handler completes by returning the value of the handler block in place of the value of the protected block (here [x/y]). We can exit the current method by putting an explicit return inside the handler block
  • 6. S.Ducasse 6 The Main Exceptions of VW Exception class Exceptional Event Default Action Error Any program error Open a Notifier a Notifier ArithmeticError Any error evaluating an arithmetic Inherited from Error MessageNotUnderstood Any unusual event that does not impair continued execution of the program Inherited from Error Notification Notification Any unusual event that does not impair continued execution of the program Do nothing continuing executing Do nothing continuing executing An unusual event that Display Yes/No dialog
  • 7. S.Ducasse 7 Exception Sets [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] Exception Sets
  • 8. S.Ducasse 8 Each process has its own exception environment: an ordered list of active handlers. Process starts -> list empty [aaaa] on: Error do: [bbb] -> Error,bbb added to the beginning of the list When an exception is signaled, the system sends a message to the first handler of the exception handler. If the handler cannot handle the exception, the next one is asked If no handler can handle the exception then the default action is performed Exception Environment 8
  • 9. S.Ducasse 9 A handler block completes by executing the last statement of the block. The value of the last statement is then the value returned by the handler block. Where this value should be returned depends: Nonresumable (Error) Sq: ([Error signal. ‘Value from protected block’] on: Error do: [:ex|ex return: ‘Value from handler’]) > ‘Value from handler’ Resumable and Non-Resumable (i)
  • 10. S.Ducasse 10 Resumable (Warning, Notification) In this case Notification signal raises an exception, then the context is restored and the value returned normally [Notification raiseSignal. ‘Value from protected block’] on: Notification do: [:ex|ex resume: ‘Value from handler’] [Notification signal. 'Value from protected block'] on: Notification do: [:ex|ex resume: 'Value from handler'] > ‘Value from protected Block’. Resumable and Non-Resumable (ii)
  • 11. S.Ducasse 11 Transcript show: [Notification raiseSignal. 'Value from protected block'] on: Notification do: [:ex| Transcript show: 'Entering handler '. 'Value from handler'. '5'] -> Entering handler 5 Resume:/Return:
  • 12. S.Ducasse 12 Transcript show: [Notification raiseSignal. 'Value from protected block'] on: Notification do: [:ex| Transcript show: 'Entering handler '. ex resume: 'Value from handler'. '5'] > Entering handler Value from protected block Transcript show: [Notification raiseSignal. 'Value from protected'] on: Notification do: [:ex| Transcript show: 'Entering handler '. ex return: 'Value from handler'. '5'] -> Entering handler Value from handler Resume:/Return:
  • 13. S.Ducasse 13 exit or exit: (VW specific) Resumes on a resumable and returns on a nonresumable exception resume or resume: Attempts to continue processing the protected block, immeditely following the message that triggered the exception. return or return: ends processing the protected block that triggered the exception retry re-evaluates the protected block retryUsing: evaluates a new block in place of the protected block Exiting Handlers Explicitly
  • 14. S.Ducasse 14 resignalAs: resignal the exception as another on pass exit the current handler and pass to the next outer handler, control does not return to the passer outer as with pass, except will regain control if the outer handler resumes exit:, resume: and return: return their argument as the return value, instead of the value of the final statement of the handler block Exiting Handlers Explicitly (ii)
  • 15. S.Ducasse 15 Look in Exception class examples categories -2.0 to: 2.0 do: [ :i | [ 10.0 / i. Transcript cr; show: i printString ] on: Number divisionByZeroSignal do: [:ex | Transcript cr; show: 'divideByZero abort'. ex return ] ] -2.0 -1.0 divideByZero abort 1.0 2.0 Examples
  • 16. S.Ducasse 16 retry recreates the exception environment of active handlers [ x /y] on: ZeroDivide do: [:exception| y := 0.00001. exception retry] Examples