Alialksandr
Lomau
VETERANT
EL “CAPITAN” DEPLOY
https://speakerdeck.com/allomov
https://twitter.com/code1n
Slide 3
Slide 3 text
They can be everywhere!
Slide 4
Slide 4 text
No content
Slide 5
Slide 5 text
No content
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
No content
Slide 8
Slide 8 text
These Pokemons Are
...
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
Variable is not
initialized
Slide 14
Slide 14 text
Variable is not
initialized
Wrong argument
type
Slide 15
Slide 15 text
Variable is not
initialized
Wrong argument
type
File does
not exist
Slide 16
Slide 16 text
Variable is not
initialized
Wrong argument
type
File does
not exist
Not enough
memory
Slide 17
Slide 17 text
Variable is not
initialized
Wrong argument
type
File does
not exist
Not enough
memory
water pokemon is definitely
a memory leak
Slide 18
Slide 18 text
Ruby == Minefield
Slide 19
Slide 19 text
Ruby == Minefield
Slide 20
Slide 20 text
Catche Pokemon
with Pokeball
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
== Exception Class
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
rescue SomeError => e
# ...
end
rescue
Slide 25
Slide 25 text
rescue SomeError, SomeOtherError => e
# ...
end
multiple rescue
Slide 26
Slide 26 text
rescue SomeError => e
# ...
rescue SomeOtherError => e
# ...
end
stacking rescue
Slide 27
Slide 27 text
rescue
rescue
# ...
end
# is equivalent to:
rescue StandardError
# ...
end
Slide 28
Slide 28 text
rescue
Slide 29
Slide 29 text
rescue
rescue Exception => e
# ...
end
Slide 30
Slide 30 text
rescue
rescue => error
# ...
end
# is equivalent to:
rescue StandardError => error
# ...
end
Slide 31
Slide 31 text
rescue
begin
raise "Timeout while reading from socket"
rescue errors_with_message(/socket/)
puts "Ignoring socket error"
end
Slide 32
Slide 32 text
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
global variable
•$!
•$ERROR_INFO
note: set it to nil to rescue exception
Slide 40
Slide 40 text
ensure
begin
# do something
raise 'An error has occured.'
rescue => e
puts 'I am rescued.'
ensure
puts 'This code always is executed.'
end
Slide 41
Slide 41 text
retry
tries = 0
begin
tries += 1
puts "Trying #{tries}..."
raise "Didn't work"
rescue
retry if tries < 3
puts "I give up"
end
Slide 42
Slide 42 text
else
begin
yield
rescue
puts "Only on error"
else
puts "Only on success"
ensure
puts "Always executed"
end
Slide 43
Slide 43 text
• 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