SlideShare a Scribd company logo
Ruby Exceptions
Array.hello
#NoMethodError: undefined method `hello' for Array:Class
#from (irb):3
#from /home/vladimir/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
hello
#NameError: undefined local variable or method `hello' for main:Object
#from (irb):4
#from /home/vladimir/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
Ruby Exceptions
def my_method()
raise "SomeError message ..." # raise "m" == raise RuntimeError.new("m")
end
my_method
#exceptions.rb:2:in `my_method': SomeError message ... (RuntimeError)
#from exceptions.rb:5:in `<main>'
class Book
attr_accessor :author, :page_count, :title
def initialize(args = {})
@author = args[:author]
self.page_count = args[:page_count]
@title = args[:title]
end
def page_count=(value)
min, max = 1, 1000
if value < min || value > max
raise RangeError, "Value of [#{value}] outside bounds of [#{min}] to
[#{max}]."
else
@page_count = value
end
end
end
begin
100 / 0
rescue
puts "F! Divider is zero!"
end
#=> F! Divider is zero!
begin
some_undefined_method_call
rescue NameError
puts "F! Undefined method!"
end
#=>F! Undefined method!
def hello(msg = "")
raise "Empty message!" if msg == ""
puts(msg)
rescue
puts "Some Error!"
end
hello("Rubydev.ru") #Rubydev.ru
hello #Some Error!
begin
some_undefined_method_call
rescue NameError
p "F! Undefined method!"
ensure
p "RubyDev.ru"
end
#=>"F! Undefined method!"
#=>"RubyDev.ru"
begin
3 / 0
rescue ZeroDivisionError => e
puts "#{e.class}: #{e.message}"
end
begin
"my string".odd?
rescue NoMethodError => e
puts "#{e.class}: #{e.message}"
end
class MyError < StandardError
attr_reader :thing
def initialize(msg="My default message", thing="apple")
@thing = thing
super(msg)
end
end
begin
raise MyError.new("my message", "my thing")
rescue => e
puts e.thing # "my thing"
end

More Related Content

PDF
Lazy Data Using Perl
PDF
A piece of sugar in your client-side development
PDF
Découplez votre appli en micro-APIs
PDF
Orlando BarCamp Why Javascript Doesn't Suck
PPTX
The Power of F#
PDF
A Self Replicating Serverless Function
PDF
Ruby and Rails by example
PDF
Ruby on Rails Presentation
Lazy Data Using Perl
A piece of sugar in your client-side development
Découplez votre appli en micro-APIs
Orlando BarCamp Why Javascript Doesn't Suck
The Power of F#
A Self Replicating Serverless Function
Ruby and Rails by example
Ruby on Rails Presentation

What's hot (16)

PDF
PPTX
Ruby Metaprogramming
PDF
Александр Трищенко: PHP 7 Evolution
PDF
How to work with legacy code
PDF
Ruby on Rails for beginners
KEY
My Development Story
PPTX
Object oriented php
PDF
Web 4 | Core JavaScript
PPTX
A Blink Into The Rails Magic
PDF
Web 8 | Introduction to PHP
PDF
De 0 a 100 con Bash Shell Scripting y AWK
TXT
Xmpp prebind
KEY
A tour on ruby and friends
PPTX
Node.js for PHP developers
PDF
Introduction to Ruby
Ruby Metaprogramming
Александр Трищенко: PHP 7 Evolution
How to work with legacy code
Ruby on Rails for beginners
My Development Story
Object oriented php
Web 4 | Core JavaScript
A Blink Into The Rails Magic
Web 8 | Introduction to PHP
De 0 a 100 con Bash Shell Scripting y AWK
Xmpp prebind
A tour on ruby and friends
Node.js for PHP developers
Introduction to Ruby
Ad

Similar to Ruby Exceptions (20)

PPTX
Introduction to Ruby’s Reflection API
KEY
Power Ruby
PPT
Advanced Ruby
PDF
Ruby 2.0
PDF
Slides chapter3part1 ruby-forjavaprogrammers
PDF
Dangerous Ruby (or: Job Security)
PDF
Metaprogramming in Ruby
KEY
Metaprogramming
PDF
Catch and Throw in Ruby
PDF
Ruby19 osdc-090418222718-phpapp02
PDF
Ruby closures, how are they possible?
KEY
Ruby objects
PPTX
7 Methods and Functional Programming
PPTX
Ruby from zero to hero
PDF
Ruby Intro {spection}
PDF
Ruby 程式語言入門導覽
PDF
Hidden Gems of Ruby 1.9
PPTX
Power ruby
PDF
Test-driven Development no Rails - Começando com o pé direito
PDF
Ruby 1.9
Introduction to Ruby’s Reflection API
Power Ruby
Advanced Ruby
Ruby 2.0
Slides chapter3part1 ruby-forjavaprogrammers
Dangerous Ruby (or: Job Security)
Metaprogramming in Ruby
Metaprogramming
Catch and Throw in Ruby
Ruby19 osdc-090418222718-phpapp02
Ruby closures, how are they possible?
Ruby objects
7 Methods and Functional Programming
Ruby from zero to hero
Ruby Intro {spection}
Ruby 程式語言入門導覽
Hidden Gems of Ruby 1.9
Power ruby
Test-driven Development no Rails - Começando com o pé direito
Ruby 1.9
Ad

More from Masters Academy (20)

PPTX
Basic Net technologies
PPTX
Databases
PPTX
Environment
PPTX
Frontend
PPTX
Development Methodologies
PPTX
Object-Oriented Programming
PPTX
PPTX
Processing
PPTX
Serialization
PPTX
Serverless
PPTX
Data Types
PPTX
How to be up todate
PPTX
Call stack, event loop and async programming
PPTX
Html, css, js
PPTX
Server architecture
PPTX
Serialization
PPTX
Data types
PPTX
Net Technologies
PPTX
Net Technologies
Basic Net technologies
Databases
Environment
Frontend
Development Methodologies
Object-Oriented Programming
Processing
Serialization
Serverless
Data Types
How to be up todate
Call stack, event loop and async programming
Html, css, js
Server architecture
Serialization
Data types
Net Technologies
Net Technologies

Recently uploaded (20)

PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Lesson notes of climatology university.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
O7-L3 Supply Chain Operations - ICLT Program
Lesson notes of climatology university.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
human mycosis Human fungal infections are called human mycosis..pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
102 student loan defaulters named and shamed – Is someone you know on the list?
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...

Ruby Exceptions

  • 2. Array.hello #NoMethodError: undefined method `hello' for Array:Class #from (irb):3 #from /home/vladimir/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>' hello #NameError: undefined local variable or method `hello' for main:Object #from (irb):4 #from /home/vladimir/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
  • 4. def my_method() raise "SomeError message ..." # raise "m" == raise RuntimeError.new("m") end my_method #exceptions.rb:2:in `my_method': SomeError message ... (RuntimeError) #from exceptions.rb:5:in `<main>'
  • 5. class Book attr_accessor :author, :page_count, :title def initialize(args = {}) @author = args[:author] self.page_count = args[:page_count] @title = args[:title] end def page_count=(value) min, max = 1, 1000 if value < min || value > max raise RangeError, "Value of [#{value}] outside bounds of [#{min}] to [#{max}]." else @page_count = value end end end
  • 6. begin 100 / 0 rescue puts "F! Divider is zero!" end #=> F! Divider is zero!
  • 7. begin some_undefined_method_call rescue NameError puts "F! Undefined method!" end #=>F! Undefined method! def hello(msg = "") raise "Empty message!" if msg == "" puts(msg) rescue puts "Some Error!" end hello("Rubydev.ru") #Rubydev.ru hello #Some Error!
  • 8. begin some_undefined_method_call rescue NameError p "F! Undefined method!" ensure p "RubyDev.ru" end #=>"F! Undefined method!" #=>"RubyDev.ru"
  • 9. begin 3 / 0 rescue ZeroDivisionError => e puts "#{e.class}: #{e.message}" end begin "my string".odd? rescue NoMethodError => e puts "#{e.class}: #{e.message}" end
  • 10. class MyError < StandardError attr_reader :thing def initialize(msg="My default message", thing="apple") @thing = thing super(msg) end end begin raise MyError.new("my message", "my thing") rescue => e puts e.thing # "my thing" end