Ruby on Rails Developers Forum

For all Ruby on Rails (RoR) Expert Developers Programmers Coders
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

rescue-raise VS catch-throw in Ruby

 
Post new topic   Reply to topic    Ruby on Rails Developers Forum Index -> The Mechanics of Ruby programming
View previous topic :: View next topic  
Author Message
Rohini Pamarty



Joined: 12 May 2011
Posts: 53

PostPosted: Thu May 26, 2011 10:33 am    Post subject: rescue-raise VS catch-throw in Ruby Reply with quote

rescue and raise
_________________

In Ruby, exceptions are packaged into objects of class Exception or one of Exception’s many subclasses.Ruby can raise exceptions automatically when incorrect functions are performed, and can raise exceptions from code too which can be done using the "raise" method and by using an existing exception class, or by creating a new own that inherits from the existing Exception class.
In most situations, stopping a program because of a single error isn’t necessary. In Ruby, the "rescue" clause is used, along with begin and end, to define blocks of code to handle exceptions.

Consider the following code

class Alphabet_exception < RuntimeError
end
begin
puts "Enter a number"
num=gets.chomp
raise Alphabet_exception,"Enter only numbers" if num.match(/[a-z]/)
num=num.to_i
raise ZeroDivisionError,"Number should not be zero" if num==0
puts "value after division is #{(10/num.to_f)}"
rescue =>e
puts e
retry
end

catch and throw
___________________

Sometimes to be able to break out of a thread of execution during normal operation in a similar way to an exception, but without actually generating an error. Ruby provides two methods, catch and throw, for this purpose.

Consider following code:

catch(:finish) do
puts "enter a number"
num=gets.chomp.to_i
15.times do |x|
y = x*num
puts y
throw :finish if y == 30
end
puts "Generated numbers without 30"
end

Within the catch block numbers are generated and displayed, and if number is ever 30, control gets escape out of the block using throw :finish.


Basic difference between catch-throw and rescue-raise are

1)catch and throw are designed to be used in situations where no error has occurred, but being escaped from nested loop, method call, or similar.

2)catch and throw work with symbols rather then exceptions. As in above example ":finish" is a symbol used, whereas for rescue and raise "RuntimeError" and "ZeroDivisionError" Exception classes are used.


Thanks
Rohini
Back to top
View user's profile Send private message
xcfrr_dvd



Joined: 28 Feb 2012
Posts: 22

PostPosted: Tue Feb 28, 2012 3:15 am    Post subject: Reply with quote

Basic difference between catch-throw and rescue-raise are





------------------------

Back to top
View user's profile Send private message
xcfrr_dvd



Joined: 28 Feb 2012
Posts: 22

PostPosted: Wed Feb 29, 2012 9:43 am    Post subject: Reply with quote

situated in top of the foot, and also the brand new style from the Upside down Cone




------------------------


Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Ruby on Rails Developers Forum Index -> The Mechanics of Ruby programming
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts star wars trilogy 6 pretty little liars burn notice burn no
0 htachtac 80 Wed May 16, 2012 9:45 am
htachtac View latest post
No new posts the golden girls friends doctor who one tree hill northern e
0 htachtac 51 Wed May 16, 2012 9:44 am
htachtac View latest post
No new posts I Love It How I Met Your Mother
0 joy89 85 Tue May 15, 2012 9:28 am
joy89 View latest post
No new posts What Have You Learned from 2 Broke Girls Season 1 DVD
0 xcfrr_dvd 72 Tue May 15, 2012 6:12 am
xcfrr_dvd View latest post
No new posts United States of Tara Seasons 1 3 DVD Helps You Balance Your
0 xcfrr_dvd 67 Tue May 15, 2012 3:59 am
xcfrr_dvd View latest post
No new posts Great MASH seasons collection at a good price
0 marklover 76 Mon May 14, 2012 8:59 am
marklover View latest post
No new posts Enjoy Game of Thrones Season 1 DVD
0 xcfrr_dvd 61 Mon May 14, 2012 3:02 am
xcfrr_dvd View latest post
No new posts The Walking Dead Season 2 DVD Boxset Will Frightens You
0 xcfrr_dvd 63 Mon May 14, 2012 2:51 am
xcfrr_dvd View latest post
No new posts Have A Good Time of Friday Night Lights Seasons 1-5 DVD Boxs
0 xcfrr_dvd 112 Sat May 12, 2012 3:26 am
xcfrr_dvd View latest post
No new posts 2 Broke Girls Season 1 DVD Inspires You
0 xcfrr_dvd 98 Sat May 12, 2012 3:23 am
xcfrr_dvd View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More