Exceptions
Hernán A. Wilkinson
@hernanwilkinson – www.10pines.com
agile software development & services
Where do they came from?
Exceptions: Why, When, How and Where!
Exceptions: Why, When, How and Where!
What is the problem?
Repeated code!
Code != Text
Code != Text
Repeated Code = Repeated patterns of
collaboration
What does it mean to have
“repeated code”?
Repeated Code Means
● Lack of abstraction
● In OO terms: Lack of an Object
How do we remove it?
Removing Repeated Code
1. Contextual-Move repeated code to some “place”
2. Parameterize what changes (including types)
3. NAME IT!  The most important step
4. Use it :-)
Contextual Move
What changes?
Exceptions: Why, When, How and Where!
How do we
parameterize it?
Polymorphism
How do we
parameterize it?
Closure
Name it! – The most important step
And if you are a good ruby programmer
Exceptions: Why, When, How and Where!
Error Codes
What is the problem?
Exceptions: Why, When, How and Where!
And now, what is the problem?
Repeated Code!
Exceptions: Why, When, How and Where!
Better, but…
Repeated code!
Exceptions: Why, When, How and Where!
Cool! No repeated code!
No way to forget to handle the error!
Much nicer!
But… I don’t want to panic! I want to stop
execution and return the error!
Exceptions: Why, When, How and Where!
Will stop execution?
Full Closure to the rescue!!
Ruby
What will be printed?
15
10
Only if we could do this in Go…
What are Exceptions?
Exceptions
Conceptually
An indication that a ”Contract” has been violated
Pragmatically
Abstraction that removes “repeated code” from the “error code technique”
Contract &
Contract Violation
Implicit
Contract Type
Explicit
Contract Definition
Contract Definition
Pre-conditions
Contract Definition
Pre-conditions
Post-conditions
Contract Definition
Pre-conditions
Post-conditions
Invariant
Contract Definition
Extract method!
Contract Definition
Contract Definition
Extract method!
Contract Definition
Contract Definition
Extract method!
Contract Definition
Contract Definition
Generalization
Contract Definition
Contract Definition
Too expensive!
Contract Definition
We convert them to tests!
Contract Definition
What do we do with it?
Contract Definition – Pre-Conditions
The sender has to satisfy it, the receiver assumes
the sender is “nice and cool”
C school (Bell Labs/Berkeley)
Contract Definition – Pre-Conditions
The receiver checks the sender complies with it.
The receiver does not trust the sender
Lisp school (MIT)
Contract Definition
Lisp school (MIT) wins! – Always check pre-conditions
When to raise an Exception?
When a contract is violated
In the business code: When a pre-condition is not meet
In the tests: When a post-condition or invariant is not meet
Who raises exceptions?
object 1
object 2
object 3
object 4
object 5
object 6
object 7
m5
m2
m3
m4
m6
m1
Who raises exceptions?
object 1
object 2
object 3
object 4
object 5
object 6
object 7
m5
m2
m3
m4
m6
m1
The ones at the
bottom because they
are the ones that do
the real stuff
Who handles exceptions?
object 1
object 2
object 3
object 4
object 5
object 6
object 7
m5
m2
m3
m4
m6
m1
Who handles exceptions?
object 1
object 2
object 3
object 4
object 5
object 6
object 7
m5
m2
m3
m4
m6
m1
The ones at the
top because they
have all the
context of what is
going on
How to Handle Exceptions
• Let’s see some examples
How to Handle Exceptions
• Unexpected Exceptions  nothing to do!
• Expected Exceptions  the ones that can be handled
• If they are expected, are they exceptions?
• Would not be better to have parameters to pass handler for the expected
“exceptions”?
How to Handle Exceptions
NEVER!!!
Which Exception should be raise?
• One per “error type”?
• CanNotWithdrawException, CanNotPrintException,
CanNotOpenFileException, etc.
• Always the same, ex. Exception?
• It depends?
Which Exception should be raise?
• If the exception will not be handled, use Exception
• If the exception will be handled, use a specific one
• If you are writing a framework/library  you don’t know how they
will be handled by users  over design
• If you ”own” the code (ex. Teespring)  create new ones as need it to
handle them
Implementation!
Implementation
● A Fully Object-Oriented Exception Handling System (Christophe Dony)
● Implementing Exceptions with Ruby:
https://www.youtube.com/playlist?list=PLMkq_h36PcLAE5CPRGG5xd62KVJABPQdd
Thanks!
Hernán A. Wilkinson
@hernanwilkinson – www.10pines.com
agile software development & services

More Related Content

PPTX
TDD & Refactoring
PPTX
Go/Ruby/Java: What's next?
PPT
The ten commandments of TDD
PPT
Los diez mandamientos de TDD
PDF
Basics of writing clean code
PDF
Tdd in practice
PDF
Test Driven Development en Go con Ginkgo y Gomega
PPTX
Clean code
TDD & Refactoring
Go/Ruby/Java: What's next?
The ten commandments of TDD
Los diez mandamientos de TDD
Basics of writing clean code
Tdd in practice
Test Driven Development en Go con Ginkgo y Gomega
Clean code

What's hot (20)

PPTX
Lập trình hướng kiểm thử - Test Driven development
PDF
7 lessons learned building high availability / performance systems - CM2015
PDF
TDD and Simple Design Workshop - Session 1 - November 2018
PPTX
Intro to TDD
ODP
Unit testing-patterns
ODP
Assorted TDD tips
ODP
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
PDF
Evolutionary Design - Refactoring Heuristics
PPTX
Unit Test Lab - Why Write Unit Tests?
PDF
Run your project like it's an OpenSource project
PPTX
Clean Code, Resumen Capítulo 1.
PDF
Test driven development - why you should test first?
PPTX
TDD with RSpec
PPTX
Code Smells - Refactoring
PDF
I'm a TDD cheat and I'm not afraid to admit it
PDF
Day1 - TDD (Lecture SS 2015)
PDF
Code Review Matters and Manners
PPTX
Unit testing JS = SQLSat 324
PPTX
Basic software engineering principles - Session 1
PPTX
Basic software engineering principles with code examples - Session 2
Lập trình hướng kiểm thử - Test Driven development
7 lessons learned building high availability / performance systems - CM2015
TDD and Simple Design Workshop - Session 1 - November 2018
Intro to TDD
Unit testing-patterns
Assorted TDD tips
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Evolutionary Design - Refactoring Heuristics
Unit Test Lab - Why Write Unit Tests?
Run your project like it's an OpenSource project
Clean Code, Resumen Capítulo 1.
Test driven development - why you should test first?
TDD with RSpec
Code Smells - Refactoring
I'm a TDD cheat and I'm not afraid to admit it
Day1 - TDD (Lecture SS 2015)
Code Review Matters and Manners
Unit testing JS = SQLSat 324
Basic software engineering principles - Session 1
Basic software engineering principles with code examples - Session 2

Similar to Exceptions: Why, When, How and Where! (20)

PDF
高品質軟體的基本動作 101 for NTHU
PDF
Workshop fight legacy code write unit test
PDF
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
PDF
高品質軟體的基本動作 101 + 102 for NUU
PPTX
Principled And Clean Coding
PPT
Agile latvia evening_unit_testing_in_practice
PDF
Working Effectively with Legacy Code: Lessons in Practice
PDF
Leaping over the Boundaries of Boundary Value Analysis
PDF
Exception Handling: Designing Robust Software in Ruby (with presentation note)
PPTX
PDF
Test-Driven Development
PDF
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
PPTX
Writing testable code
PDF
Beyond Agile Testing to Lean Development — Rakuten Technology Conference
PDF
Unleashing the power of Unit Testing - Franck Ninsabira.pdf
KEY
Testing gone-right
DOC
Manual Testing.
PDF
YAGNI Principle and Clean Code
PDF
The 30-Month Migration
PPTX
Cinci ug-january2011-anti-patterns
高品質軟體的基本動作 101 for NTHU
Workshop fight legacy code write unit test
[XPday.vn] Legacy code workshop (at) [XP Day Vietnam 2015]
高品質軟體的基本動作 101 + 102 for NUU
Principled And Clean Coding
Agile latvia evening_unit_testing_in_practice
Working Effectively with Legacy Code: Lessons in Practice
Leaping over the Boundaries of Boundary Value Analysis
Exception Handling: Designing Robust Software in Ruby (with presentation note)
Test-Driven Development
Aki Salmi - Refactoring legacy code: a true story @ I T.A.K.E. Unconference 2...
Writing testable code
Beyond Agile Testing to Lean Development — Rakuten Technology Conference
Unleashing the power of Unit Testing - Franck Ninsabira.pdf
Testing gone-right
Manual Testing.
YAGNI Principle and Clean Code
The 30-Month Migration
Cinci ug-january2011-anti-patterns

More from Hernan Wilkinson (20)

PDF
Hacia una síntesis de diseño a partir de entender qué es modelar con software
PDF
Live Typing - California Smalltalkers
PDF
Buenos Aires vs. (London vs. Chicago) Agiles 2020
PPTX
LiveTyping - Anotación automática de tipos para lenguajes dinámicos
PPTX
LiveTyping: Update and What is next
PPTX
Cuis smalltalk past present and future
PPTX
Live Typing - Automatic Type Annotation that improves the Programming eXperie...
PPTX
El Desarrollo de Software como debería Ser - PyConAr 2018
PPTX
Lessons Learned Implementing Refactorings
PPTX
Dynamic Type Information
PPTX
El Desarrollo de Software como debería Ser - Nerdear.la 2018
PDF
El Desarrollo de Software como debería Ser
PPTX
CuisUniversity
PPTX
Oop is not Dead
PPT
Augmenting Smalltalk Syntax
PDF
Growing an open participative horizontal and based on trust company
PDF
Como escribir buenos tests al hacer TDD
PPT
Desarrollando sistemas con metodologías y técnicas agiles
PPT
Técnicas y herramientas para que la computadora haga más y el programador m...
PPTX
Obejct Oriented SCM - OOSCM
Hacia una síntesis de diseño a partir de entender qué es modelar con software
Live Typing - California Smalltalkers
Buenos Aires vs. (London vs. Chicago) Agiles 2020
LiveTyping - Anotación automática de tipos para lenguajes dinámicos
LiveTyping: Update and What is next
Cuis smalltalk past present and future
Live Typing - Automatic Type Annotation that improves the Programming eXperie...
El Desarrollo de Software como debería Ser - PyConAr 2018
Lessons Learned Implementing Refactorings
Dynamic Type Information
El Desarrollo de Software como debería Ser - Nerdear.la 2018
El Desarrollo de Software como debería Ser
CuisUniversity
Oop is not Dead
Augmenting Smalltalk Syntax
Growing an open participative horizontal and based on trust company
Como escribir buenos tests al hacer TDD
Desarrollando sistemas con metodologías y técnicas agiles
Técnicas y herramientas para que la computadora haga más y el programador m...
Obejct Oriented SCM - OOSCM

Recently uploaded (20)

PDF
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
PPTX
Relevance Tuning with Genetic Algorithms
PDF
Mobile App for Guard Tour and Reporting.pdf
PPTX
MCP empowers AI Agents from Zero to Production
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PPTX
Folder Lock 10.1.9 Crack With Serial Key
PPTX
Beige and Black Minimalist Project Deck Presentation (1).pptx
PPTX
SAP Business AI_L1 Overview_EXTERNAL.pptx
PPTX
Why 2025 Is the Best Year to Hire Software Developers in India
PDF
solman-7.0-ehp1-sp21-incident-management
PDF
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
PPTX
ESDS_SAP Application Cloud Offerings.pptx
PPTX
Foundations of Marketo Engage: Nurturing
PPTX
AI Tools Revolutionizing Software Development Workflows
PDF
Ragic Data Security Overview: Certifications, Compliance, and Network Safegua...
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PPTX
Presentation - Summer Internship at Samatrix.io_template_2.pptx
PPTX
Post-Migration Optimization Playbook: Getting the Most Out of Your New Adobe ...
PDF
Module 1 - Introduction to Generative AI.pdf
PPTX
Streamlining Project Management in the AV Industry with D-Tools for Zoho CRM ...
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
Relevance Tuning with Genetic Algorithms
Mobile App for Guard Tour and Reporting.pdf
MCP empowers AI Agents from Zero to Production
ROI from Efficient Content & Campaign Management in the Digital Media Industry
Folder Lock 10.1.9 Crack With Serial Key
Beige and Black Minimalist Project Deck Presentation (1).pptx
SAP Business AI_L1 Overview_EXTERNAL.pptx
Why 2025 Is the Best Year to Hire Software Developers in India
solman-7.0-ehp1-sp21-incident-management
Multiverse AI Review 2025_ The Ultimate All-in-One AI Platform.pdf
ESDS_SAP Application Cloud Offerings.pptx
Foundations of Marketo Engage: Nurturing
AI Tools Revolutionizing Software Development Workflows
Ragic Data Security Overview: Certifications, Compliance, and Network Safegua...
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Presentation - Summer Internship at Samatrix.io_template_2.pptx
Post-Migration Optimization Playbook: Getting the Most Out of Your New Adobe ...
Module 1 - Introduction to Generative AI.pdf
Streamlining Project Management in the AV Industry with D-Tools for Zoho CRM ...

Exceptions: Why, When, How and Where!