SlideShare a Scribd company logo
1 of 53
Alialksandr
Lomau
VETERANT
EL “CAPITAN” DEPLOY
https://speakerdeck.com/allomov
https://twitter.com/code1n
They can be everywhere!
These Pokemons Are
...
Variable is not
initialized
Variable is not
initialized
Wrong argument
type
Variable is not
initialized
Wrong argument
type
File does
not exist
Variable is not
initialized
Wrong argument
type
File does
not exist
Not enough
memory
Variable is not
initialized
Wrong argument
type
File does
not exist
Not enough
memory
water pokemon is definitely
a memory leak
Ruby == Minefield
Ruby == Minefield
Catche Pokemon
with Pokeball
== Exception Class
rescue SomeError => e
# ...
end
rescue
rescue SomeError, SomeOtherError => e
# ...
end
multiple rescue
rescue SomeError => e
# ...
rescue SomeOtherError => e
# ...
end
stacking rescue
rescue
rescue
# ...
end
# is equivalent to:
rescue StandardError
# ...
end
rescue
rescue
rescue Exception => e
# ...
end
rescue
rescue => error
# ...
end
# is equivalent to:
rescue StandardError => error
# ...
end
rescue
begin
raise "Timeout while reading from socket"
rescue errors_with_message(/socket/)
puts "Ignoring socket error"
end
rescue
def errors_with_message(pattern)
m = Module.new
m.singleton_class.instance_eval do
define_method(:===) do |e|
pattern === e.message
end
end
m
end
raise
raise(exception_class_or_object,
message,
backtrace)
raise
raise
# is equivalent to:
raise RuntimeError
raise
raise 'An error has occured.'
# is equivalent to :
raise RuntimeError, 'An error has occured.'
raise
raise RuntimeError, 'An error has occured.'
# is equivalent to :
raise RuntimeError.new('An error has occured.')
raise
raise RuntimeError, 'An error'
# is equivalent to :
raise RuntimeError, 'An error', caller(0)
caller
2.4.2 :005 > puts caller(0).join("n")
(irb):5:in `irb_binding'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/workspace.rb:87:in `eval'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/workspace.rb:87:in `evaluate'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/context.rb:381:in `evaluate'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:493:in `block (2 levels) in eval_input'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:627:in `signal_status'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:490:in `block in eval_input'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/ruby-lex.rb:246:in `block (2 levels) in
each_top_level_statement'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/ruby-lex.rb:232:in `loop'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/ruby-lex.rb:232:in `block in each_top_level_sta
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/ruby-lex.rb:231:in `catch'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb/ruby-lex.rb:231:in `each_top_level_statement'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:489:in `eval_input'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:430:in `block in run'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:429:in `catch'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:429:in `run'
/Users/allomov/.rvm/rubies/ruby-2.4.2/lib/ruby/2.4.0/irb.rb:385:in `start'
/Users/allomov/.rvm/rubies/ruby-2.4.2/bin/irb:11:in `<main>'
=> nil
global variable
•$!
•$ERROR_INFO
note: set it to nil to rescue exception
ensure
begin
# do something
raise 'An error has occured.'
rescue => e
puts 'I am rescued.'
ensure
puts 'This code always is executed.'
end
retry
tries = 0
begin
tries += 1
puts "Trying #{tries}..."
raise "Didn't work"
rescue
retry if tries < 3
puts "I give up"
end
else
begin
yield
rescue
puts "Only on error"
else
puts "Only on success"
ensure
puts "Always executed"
end
• Use exception when you need
• Wrap exception when re-raising
• Avoid raising during ensure
• Exception is your method interface too
• Classify your exceptions
• Readable exceptional code
• Declare classes for app exception
good points
Benchmarks
Circuit Breaker pattern
Anti-Patterns
• Exceptions controlled flow
• rescue nil
What to do with errors?
• handle them
• send emails with them
• send them to error report system
• errbit
• pappertrail
• elk
• … etc.
Alternatives
• Callbacks on fails (JavaScript)
• Promises (JavaScript)
• Returning codes (golang)
To Know More
throw "End"

More Related Content

Similar to Ruby Exceptions Guide

Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyWen-Tien Chang
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developersAndrew Eddie
 
Aprendendo solid com exemplos
Aprendendo solid com exemplosAprendendo solid com exemplos
Aprendendo solid com exemplosvinibaggio
 
Monkeybars in the Manor
Monkeybars in the ManorMonkeybars in the Manor
Monkeybars in the Manormartinbtt
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)RORLAB
 
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberlCukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberlSkills Matter
 
Exceptions in Ruby - Tips and Tricks
Exceptions in Ruby - Tips and TricksExceptions in Ruby - Tips and Tricks
Exceptions in Ruby - Tips and TricksDimelo R&D Team
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Michelangelo van Dam
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3juliangiuca
 
Write your Ruby in Style
Write your Ruby in StyleWrite your Ruby in Style
Write your Ruby in StyleBhavin Javia
 
Ecossistema Ruby - versão SCTI UNF 2013
Ecossistema Ruby - versão SCTI UNF 2013Ecossistema Ruby - versão SCTI UNF 2013
Ecossistema Ruby - versão SCTI UNF 2013Fabio Akita
 
Akka and the Zen of Reactive System Design
Akka and the Zen of Reactive System DesignAkka and the Zen of Reactive System Design
Akka and the Zen of Reactive System DesignLightbend
 

Similar to Ruby Exceptions Guide (20)

Language supports it
Language supports itLanguage supports it
Language supports it
 
Exception Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in RubyException Handling: Designing Robust Software in Ruby
Exception Handling: Designing Robust Software in Ruby
 
Node.js for PHP developers
Node.js for PHP developersNode.js for PHP developers
Node.js for PHP developers
 
Aprendendo solid com exemplos
Aprendendo solid com exemplosAprendendo solid com exemplos
Aprendendo solid com exemplos
 
Iq rails
Iq railsIq rails
Iq rails
 
In Vogue Dynamic
In Vogue DynamicIn Vogue Dynamic
In Vogue Dynamic
 
Monkeybars in the Manor
Monkeybars in the ManorMonkeybars in the Manor
Monkeybars in the Manor
 
Active Support Core Extensions (1)
Active Support Core Extensions (1)Active Support Core Extensions (1)
Active Support Core Extensions (1)
 
Cukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberlCukeup nyc ian dees on elixir, erlang, and cucumberl
Cukeup nyc ian dees on elixir, erlang, and cucumberl
 
Exceptions in Ruby - Tips and Tricks
Exceptions in Ruby - Tips and TricksExceptions in Ruby - Tips and Tricks
Exceptions in Ruby - Tips and Tricks
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
Beginner's Sinatra
Beginner's SinatraBeginner's Sinatra
Beginner's Sinatra
 
Let's Talk Scope
Let's Talk ScopeLet's Talk Scope
Let's Talk Scope
 
Upgrading to Rails 3
Upgrading to Rails 3Upgrading to Rails 3
Upgrading to Rails 3
 
linux-namespaces.pdf
linux-namespaces.pdflinux-namespaces.pdf
linux-namespaces.pdf
 
Write your Ruby in Style
Write your Ruby in StyleWrite your Ruby in Style
Write your Ruby in Style
 
Ecossistema Ruby - versão SCTI UNF 2013
Ecossistema Ruby - versão SCTI UNF 2013Ecossistema Ruby - versão SCTI UNF 2013
Ecossistema Ruby - versão SCTI UNF 2013
 
Akka and the Zen of Reactive System Design
Akka and the Zen of Reactive System DesignAkka and the Zen of Reactive System Design
Akka and the Zen of Reactive System Design
 
Vagrant for real
Vagrant for realVagrant for real
Vagrant for real
 
Use me strict
Use me strictUse me strict
Use me strict
 

More from Rubizza

Intoduction to React
Intoduction to ReactIntoduction to React
Intoduction to ReactRubizza
 
Linux commands-effectiveness
Linux commands-effectivenessLinux commands-effectiveness
Linux commands-effectivenessRubizza
 
Variables, expressions, standard types
 Variables, expressions, standard types  Variables, expressions, standard types
Variables, expressions, standard types Rubizza
 
Hangout Utche #6. "Rambovidnaya problema"
Hangout Utche #6. "Rambovidnaya problema"Hangout Utche #6. "Rambovidnaya problema"
Hangout Utche #6. "Rambovidnaya problema"Rubizza
 
Hangout Utche #6. Math Thinking
Hangout Utche #6. Math ThinkingHangout Utche #6. Math Thinking
Hangout Utche #6. Math ThinkingRubizza
 
Rubizza #1 | Special Lecture. Vim
Rubizza #1 | Special Lecture. Vim Rubizza #1 | Special Lecture. Vim
Rubizza #1 | Special Lecture. Vim Rubizza
 
Rubizza #1 Lecture Ruby OOP
Rubizza #1 Lecture Ruby OOPRubizza #1 Lecture Ruby OOP
Rubizza #1 Lecture Ruby OOPRubizza
 

More from Rubizza (9)

Intoduction to React
Intoduction to ReactIntoduction to React
Intoduction to React
 
Linux commands-effectiveness
Linux commands-effectivenessLinux commands-effectiveness
Linux commands-effectiveness
 
Sinatra
SinatraSinatra
Sinatra
 
Variables, expressions, standard types
 Variables, expressions, standard types  Variables, expressions, standard types
Variables, expressions, standard types
 
Git
GitGit
Git
 
Hangout Utche #6. "Rambovidnaya problema"
Hangout Utche #6. "Rambovidnaya problema"Hangout Utche #6. "Rambovidnaya problema"
Hangout Utche #6. "Rambovidnaya problema"
 
Hangout Utche #6. Math Thinking
Hangout Utche #6. Math ThinkingHangout Utche #6. Math Thinking
Hangout Utche #6. Math Thinking
 
Rubizza #1 | Special Lecture. Vim
Rubizza #1 | Special Lecture. Vim Rubizza #1 | Special Lecture. Vim
Rubizza #1 | Special Lecture. Vim
 
Rubizza #1 Lecture Ruby OOP
Rubizza #1 Lecture Ruby OOPRubizza #1 Lecture Ruby OOP
Rubizza #1 Lecture Ruby OOP
 

Recently uploaded

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Ruby Exceptions Guide