Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Catch and Throw in Ruby

Catch and Throw in Ruby

Rubizza Camp Fire, July 2018

Aliaksandr Lomau

July 18, 2018
Tweet

More Decks by Aliaksandr Lomau

Other Decks in Technology

Transcript

  1. Variable is not initialized Wrong argument type File does not

    exist Not enough memory water pokemon is definitely a memory leak
  2. rescue rescue => error # ... end # is equivalent

    to: rescue StandardError => error # ... end
  3. raise raise 'An error has occured.' # is equivalent to

    : raise RuntimeError, 'An error has occured.'
  4. raise raise RuntimeError, 'An error has occured.' # is equivalent

    to : raise RuntimeError.new('An error has occured.')
  5. raise raise RuntimeError, 'An error' # is equivalent to :

    raise RuntimeError, 'An error', caller(0)
  6. 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
  7. ensure begin # do something raise 'An error has occured.'

    rescue => e puts 'I am rescued.' ensure puts 'This code always is executed.' end
  8. retry tries = 0 begin tries += 1 puts "Trying

    #{tries}..." raise "Didn't work" rescue retry if tries < 3 puts "I give up" end
  9. else begin yield rescue puts "Only on error" else puts

    "Only on success" ensure puts "Always executed" end
  10. • 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
  11. What to do with errors? • handle them • send

    emails with them • send them to error report system • errbit • pappertrail • elk • … etc.