Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Catch and Throw in Ruby
Search
Aliaksandr Lomau
July 18, 2018
Technology
71
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Catch and Throw in Ruby
Rubizza Camp Fire, July 2018
Aliaksandr Lomau
July 18, 2018
Transcript
53 slides
None
Alialksandr Lomau VETERANT EL “CAPITAN” DEPLOY https://speakerdeck.com/allomov https://twitter.com/code1n
They can be everywhere!
None
None
None
None
These Pokemons Are ...
None
None
None
None
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
None
== Exception Class
None
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)
None
None
None
To Know More
throw "End"
More Decks by Aliaksandr Lomau
See All by Aliaksandr Lomau
Fails Night
allomov
0
69
"Ya Ne Lochu Comp" Challenge.
allomov
1
110
Mortal Rubizza
allomov
0
120
Рэмбовидная проблема: особенности развития открытых технологий
allomov
0
77
Vault-tec: Safest Future
allomov
0
310
Promises and Reality
allomov
1
73
Cloud Theory for Rubizza Classes
allomov
0
110
Антология деплоя
allomov
0
98
Not For Recording: BOSH User Life Stories
allomov
0
100
Other Decks in Technology
See All in Technology
Context Ontology Accelerator 入門 (業務データの意味を、AI エージェントに渡す)
inariku
PRO
3
680
田んぼの見回りをIoTで減らしたい!水位監視から電池駆動カメラまでの試行錯誤
itosoft
1
210
JAWS_FESTA2026_オンプレ-AWS間プロキシサーバの移行_性能_障害対応で得た実践ノウハウ_高島一晃.pdf
tkazuaki0820
1
130
MCPでネットワーク障害調査を自動化する前に直すべき3つのこと
yama3133
0
240
人間+AIを「チーム」としてプロセスを再設計する
littlehands
4
2.8k
[NW-JAWS DAY]「うちのAWS、インターネット出られないんで」― AWS外の自社サービスをPrivateLinkでシュッと公開する方法
ykimi
2
440
これからのデータエンジニアの役割について考える / Rethinking the Role of Data Engineers in the AI Era
okiyuki99
0
490
Bots in Kiro Crew (One point of contact for ongoing work) | Kiro: Zero to Hero 12
inariku
PRO
1
140
大江戸Ruby会議でデータベース クライアントの話をするのは 間違っているだろうか
yahonda
1
130
An Introduction to Context Ontology Accelerator (teaching AI agents what your data means)
inariku
PRO
1
260
顧客合意を取りながら進めるAWSコスト削減の実践
optim
0
170
Intro to Kiro (One agent, every surface) | Kiro: Zero to Hero 01
inariku
PRO
2
130
Featured
See All Featured
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.6k
Mobile First: as difficult as doing things right
swwweet
225
10k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
2k
Faster Mobile Websites
deanohume
310
32k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
330
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
43k
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
Are puppies a ranking factor?
jonoalderson
2
4.2k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k