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

What's hot (16)

PDF
Slide
Naing Lin Aung
 
PPTX
Ruby Metaprogramming
Thaichor Seng
 
PDF
Александр Трищенко: PHP 7 Evolution
Oleg Poludnenko
 
PDF
How to work with legacy code
Michał Kruczek
 
PDF
Ruby on Rails for beginners
Vysakh Sreenivasan
 
KEY
My Development Story
Takahiro Fujiwara
 
PPTX
Object oriented php
jwperry
 
PDF
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
PPTX
A Blink Into The Rails Magic
Nikos Dimitrakopoulos
 
PDF
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
PDF
De 0 a 100 con Bash Shell Scripting y AWK
Adolfo Sanz De Diego
 
TXT
Xmpp prebind
Syed Arshad
 
KEY
A tour on ruby and friends
旻琦 潘
 
PPTX
Node.js for PHP developers
Andrew Eddie
 
PDF
Introduction to Ruby
Ranjith Siji
 
Ruby Metaprogramming
Thaichor Seng
 
Александр Трищенко: PHP 7 Evolution
Oleg Poludnenko
 
How to work with legacy code
Michał Kruczek
 
Ruby on Rails for beginners
Vysakh Sreenivasan
 
My Development Story
Takahiro Fujiwara
 
Object oriented php
jwperry
 
Web 4 | Core JavaScript
Mohammad Imam Hossain
 
A Blink Into The Rails Magic
Nikos Dimitrakopoulos
 
Web 8 | Introduction to PHP
Mohammad Imam Hossain
 
De 0 a 100 con Bash Shell Scripting y AWK
Adolfo Sanz De Diego
 
Xmpp prebind
Syed Arshad
 
A tour on ruby and friends
旻琦 潘
 
Node.js for PHP developers
Andrew Eddie
 
Introduction to Ruby
Ranjith Siji
 

Similar to Ruby Exceptions (20)

PDF
How to write Ruby extensions with Crystal
Anna (gaar4ica) Shcherbinina
 
ODP
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
rivierarb
 
KEY
Ruby: Beyond the Basics
Michael Koby
 
PDF
Extracting ruby gem
Yura Tolstik
 
KEY
Identify Literate Code
natedavisolds
 
PDF
Ruby 2.0
Uģis Ozols
 
PDF
Simplifying Code: Monster to Elegant in 5 Steps
tutec
 
PDF
Merb
Yehuda Katz
 
PDF
Hiveminder - Everything but the Secret Sauce
Jesse Vincent
 
KEY
Learning From Ruby (Yapc Asia)
Kang-min Liu
 
PDF
Your own (little) gem: building an online business with Ruby
Lindsay Holmwood
 
PPTX
Ruby on rails tips
BinBin He
 
PDF
JRuby hacking guide
David Calavera
 
PDF
Ruby Metaprogramming
Nando Vieira
 
PDF
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
PDF
Why Task Queues - ComoRichWeb
Bryan Helmig
 
KEY
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
PDF
mruby で mackerel のプラグインを作るはなし
Hiroshi SHIBATA
 
PDF
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 
KEY
Ruby/Rails
rstankov
 
How to write Ruby extensions with Crystal
Anna (gaar4ica) Shcherbinina
 
Ruby C extensions at the Ruby drink-up of Sophia, April 2012
rivierarb
 
Ruby: Beyond the Basics
Michael Koby
 
Extracting ruby gem
Yura Tolstik
 
Identify Literate Code
natedavisolds
 
Ruby 2.0
Uģis Ozols
 
Simplifying Code: Monster to Elegant in 5 Steps
tutec
 
Hiveminder - Everything but the Secret Sauce
Jesse Vincent
 
Learning From Ruby (Yapc Asia)
Kang-min Liu
 
Your own (little) gem: building an online business with Ruby
Lindsay Holmwood
 
Ruby on rails tips
BinBin He
 
JRuby hacking guide
David Calavera
 
Ruby Metaprogramming
Nando Vieira
 
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
Why Task Queues - ComoRichWeb
Bryan Helmig
 
Desarrollando aplicaciones web en minutos
Edgar Suarez
 
mruby で mackerel のプラグインを作るはなし
Hiroshi SHIBATA
 
Migrating Legacy Rails Apps to Rails 3
Clinton Dreisbach
 
Ruby/Rails
rstankov
 
Ad

More from Masters Academy (20)

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

Recently uploaded (20)

PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PPTX
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
PDF
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
PDF
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PPTX
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
PPTX
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PDF
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
PPTX
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PPTX
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
PPTX
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
PDF
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
PPTX
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
PDF
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Matatag Curriculum English 8-Week 1 Day 1-5.pptx
KirbieJaneGasta1
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Elo the Hero is an story about a young boy who became hero.
TeacherEmily1
 
Free eBook ~100 Common English Proverbs (ebook) pdf.pdf
OH TEIK BIN
 
CAD25 Gbadago and Fafa Presentation Revised-Aston Business School, UK.pdf
Kweku Zurek
 
PLANNING FOR EMERGENCY AND DISASTER MANAGEMENT ppt.pptx
PRADEEP ABOTHU
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Urban Hierarchy and Service Provisions.pptx
Islamic University of Bangladesh
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
The Power of Compound Interest (Stanford Initiative for Financial Decision-Ma...
Stanford IFDM
 
Comparing Translational and Rotational Motion.pptx
AngeliqueTolentinoDe
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
How Physics Enhances Our Quality of Life.pptx
AngeliqueTolentinoDe
 
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
TLE 8 QUARTER 1 MODULE WEEK 1 MATATAG CURRICULUM
denniseraya1997
 
How to Configure Refusal of Applicants in Odoo 18 Recruitment
Celine George
 
Lesson 1 : Science and the Art of Geography Ecosystem
marvinnbustamante1
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 

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