Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
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 

Exceptions in ruby

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
View previous topic :: View next topic  
Author Message
ktulasi



Joined: 21 Oct 2009
Posts: 59

PostPosted: Mon Mar 22, 2010 1:50 pm    Post subject: Exceptions in ruby Reply with quote

Exception is a variable that raised or thrown when something goes wrong.If an exception i raised the program stops executing.
The ruby interpreter knows what the problem is,but doesnt know what to do after the problem occurred.It then checks for the code that can catch it.
The code that catches the exception variable is called Exception handler.
Types of exceptions are:
1.Runtime error
2.Compile time error.

Runtime error:these are the errors which can be handled.

Compile time error: these are the errors that cannot be handled.

Exceptions that raised can be handled in ruby using,
begin rescue end

where exception raises that code must be placed betweenbegin and rescue and where the exception handled be placed in rescue and end.

ex:
---
begin
file.open "document.rb"
rescue
puts" doc not found"
end
here if document.rb file not found then it will goes to resuce and puts "doc not found"


we can place any no.of rescues in begin and end block.
when an error arise it checks for the resue clause based on exception class.

def example
my_string = "what is this error for"

my_string.nonexistentmethod
rescue Exception:
puts "error occurred"
rescue NoMethodError:
puts "You're missing that method!"
else
puts "error"
end

in the example mystring.nonexistentmethod this is calling an un existent method.so it raises an exception then it checks for its corresponding class Here Exception handles it as it is the super class of all classes.

Ruby has some predefined classes - Exception and its children - that help you to handle errors that can occur in your program...
Exception
StandardError
ArgumentError
IOError
NameError
TypeError
NoMethodError
RangeError
RegExpError

The above are the some of the predefined classes of super class Exception.

Raise:Every ruby library raises an exception if any error occurs, and you can raise exceptions explicitly in your code too. To raise an exception, use raise. It takes one argument, which should be a string that describes the exception.and the other argument as our own text message.
example:

class User
def user_details(data)
if (gender !="Female")
raise "you entered wrong"
end
end
end
a=User.new
a.user_details("male")

we can create our own exception classes.

it is like,
in one rb file take a class and inherit a predefined class

1.rb
-----
class GenderError < Exception

end

predefined class can be any class.

and in other rb file take

2.rb
----

class User
include 1.rb
def gender(data)
if gender != "Female" & gender !="Male"
raise GenderError.new , "Invalid input"
end
end
end
a=User.new
a.gender("abc")

here we are creating an object for GenderError .
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> The Mechanics of Ruby programming
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts Chat utility in a ROR application
0 shobana 2 Thu Sep 09, 2010 9:45 am
shobana View latest post
No new posts How to customize Google Maps InfoWindow using ym4r_gm
0 kalyanallampalli 54 Thu Sep 02, 2010 5:34 pm
kalyanallampalli View latest post
No new posts Drag and Drop file upload
0 umamageshwari 51 Wed Sep 01, 2010 9:36 am
umamageshwari View latest post
No new posts Flex On Rails
0 Raghu 48 Wed Sep 01, 2010 3:00 am
Raghu View latest post
No new posts MongoDB
0 Raghu 45 Tue Aug 31, 2010 3:30 pm
Raghu View latest post
No new posts Problem with Dreamweaver / java error
3 Raja 37 Tue Aug 31, 2010 3:24 pm
Raghu View latest post
No new posts Generating Model
0 phani.galla 44 Tue Aug 31, 2010 2:09 pm
phani.galla View latest post
No new posts Hide Panels using JQuery
0 Anu 48 Tue Aug 31, 2010 1:50 pm
Anu View latest post
No new posts Load a text file using ajax in jquery
0 Anu 38 Tue Aug 31, 2010 1:39 pm
Anu View latest post
No new posts Generating Migrations
0 phani.galla 31 Tue Aug 31, 2010 1:38 pm
phani.galla 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