# Errors in Ruby

**URL:** <https://www.ruby-forum.com/t/errors-in-ruby/191494>\
**Category:** Ruby\
**Created:** [July 13, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494 "2010-07-13T14:06:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 2:06pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/1 "2010-07-13T14:06:54Z")

</div>

In the following sentence: The raise method is from the Kernel module.  
By default, raise creates an exception of the RuntimeError class. From:

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.

What is meant by raising an exception of the “RunTimeError”? And, if we  
raise it on “ArgumentError”, what is mean by that?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/2 "2010-07-13T16:26:43Z")

</div>

On Jul 13, 2010, at 2:07 PM, Abder-rahman Ali wrote:

> In the following sentence: The raise method is from the Kernel module.  
> By default, raise creates an exception of the RuntimeError class. From:  
> [Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/satishtalim/ruby_exceptions.html)
> 
> What is meant by raising an exception of the “RunTimeError”? And, if we  
> raise it on “ArgumentError”, what is mean by that?

irb:0\> ArgumentError.ancestors  
=\> [ArgumentError, StandardError, Exception, Object, Kernel]  
irb:0\> RuntimeError.ancestors  
=\> [RuntimeError, StandardError, Exception, Object, Kernel]  
irb:0\> RuntimeError.superclass  
=\> StandardError  
irb:0\> ArgumentError.superclass  
=\> StandardError  
irb:0\> begin  
irb:1\* raise “foo”  
irb:1\> rescue Exception =\> e  
irb:1\> puts e.class  
irb:1\> end  
RuntimeError

Hth, Sandor  
Szücs

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:40pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/3 "2010-07-13T16:40:47Z")

</div>

On 13 Juli, 14:07, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> In the following sentence: The raise method is from the Kernel module.  
> By default, raise creates an exception of the RuntimeError class. From:[Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/satishtalim/ruby_exceptions.html)
> 
> What is meant by raising an exception of the “RunTimeError”? And, if we  
> raise it on “ArgumentError”, what is mean by that?
> 
> ## Thanks.
> 
> Posted viahttp://www.ruby-forum.com/.

Hi,

Exceptions are just regular classes in ruby. You can create your own  
or use the multitude of built in Exceptions.

Exception are usually very simple classes. The reason why you might  
need more than one is differentiation. Sometimes things can go wrong  
in more than one way.

begin

# Do stuff that raises an exception

rescue MyFirstException =\> err  
puts “MyFirstException occurred”  
rescue MySecondException =\> err  
puts “MySecondException occurred”  
end

Usually you can distinguish what an exception is supposed to represent  
by its name. RunTimeError is a kind of catch-all error, you can trap  
almost every “normal” error using this exception class. An  
ArgumentError on the other hand is more specific and is raised when  
you try to call methods using the wrong number or type of arguments.

def foo(x, y)  
puts x, y  
end

foo(3) =\> raises ArgumentError

/lasso

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:49pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/4 "2010-07-13T16:49:50Z")

</div>

Lars O. wrote:

> On 13 Juli, 14:07, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > In the following sentence: The raise method is from the Kernel module.  
> > By default, raise creates an exception of the RuntimeError class. From:[Learn How to Blog and Build Websites for Profit!](http://rubylearning.com/satishtalim/ruby_exceptions.html)
> > 
> > What is meant by raising an exception of the “RunTimeError”? And, if we  
> > raise it on “ArgumentError”, what is mean by that?
> > 
> > ## Thanks.
> > 
> > Posted viahttp://www.ruby-forum.com/.
> 
> Hi,
> 
> Exceptions are just regular classes in ruby. You can create your own  
> or use the multitude of built in Exceptions.
> 
> Exception are usually very simple classes. The reason why you might  
> need more than one is differentiation. Sometimes things can go wrong  
> in more than one way.
> 
> begin
> 
> # Do stuff that raises an exception
> 
> rescue MyFirstException =\> err  
> puts “MyFirstException occurred”  
> rescue MySecondException =\> err  
> puts “MySecondException occurred”  
> end
> 
> Usually you can distinguish what an exception is supposed to represent  
> by its name. RunTimeError is a kind of catch-all error, you can trap  
> almost every “normal” error using this exception class. An  
> ArgumentError on the other hand is more specific and is raised when  
> you try to call methods using the wrong number or type of arguments.
> 
> def foo(x, y)  
> puts x, y  
> end
> 
> foo(3) =\> raises ArgumentError
> 
> /lasso

Thanks for your reply.

I just want to ask a thing since I’m new to Ruby.

When you say: rescue MyFirstException =\> err

How do you read this =\>

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 4:50pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/5 "2010-07-13T16:50:56Z")

</div>

Thanks Sandor.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 5:07pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/6 "2010-07-13T17:07:43Z")

</div>

On 13 Juli, 16:51, Abder-Rahman A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Thanks Sandor.
> 
> Posted viahttp://www.ruby-forum.com/.

Hi,

The “=\> err” is not really needed, it just makes the error object  
available as a variable for further use. Like in:

begin  
raise “Killer robots!”  
rescue Exception =\> err  
puts “#{err.message} (#{err.class})” # prints ‘Killer robots!  
(RuntimeError)’  
end

/lasso

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 13, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/errors-in-ruby/191494/7 "2010-07-13T17:10:59Z")

</div>

Thanks a lot Lasso, that makes it clear.
