Errors in Ruby

Discussion in 'Ruby' started by Abder-Rahman Ali, Jul 13, 2010.

  1. Abder-Rahman Ali, Jul 13, 2010
    #1
    1. Advertising

  2. 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:
    > http://rubylearning.com/satishtalim/ruby_exceptions.html
    >=20
    >=20
    > 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
    =3D> [ArgumentError, StandardError, Exception, Object, Kernel]
    irb:0> RuntimeError.ancestors
    =3D> [RuntimeError, StandardError, Exception, Object, Kernel]
    irb:0> RuntimeError.superclass
    =3D> StandardError
    irb:0> ArgumentError.superclass
    =3D> StandardError
    irb:0> begin
    irb:1* raise "foo"
    irb:1> rescue Exception =3D> e
    irb:1> puts e.class
    irb:1> end
    RuntimeError


    Hth, Sandor Sz=FCcs
    --
     
    Sandor Szuecs, Jul 13, 2010
    #2
    1. Advertising

  3. Abder-Rahman Ali

    Lars Olsson Guest

    On 13 Juli, 14:07, 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: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
     
    Lars Olsson, Jul 13, 2010
    #3
  4. Lars Olsson wrote:
    > On 13 Juli, 14:07, 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: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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Abder-Rahman Ali, Jul 13, 2010
    #4
  5. Abder-Rahman Ali, Jul 13, 2010
    #5
  6. Abder-Rahman Ali

    Lars Olsson Guest

    On 13 Juli, 16:51, Abder-Rahman Ali <>
    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
     
    Lars Olsson, Jul 13, 2010
    #6
  7. Abder-Rahman Ali, Jul 13, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Errors, errors, errors

    Mark Goldin, Jan 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,027
    Mark Goldin
    Jan 17, 2004
  2. SenthilVel
    Replies:
    0
    Views:
    976
    SenthilVel
    Jun 7, 2006
  3. George1776

    Out-of-memory errors and caching errors.

    George1776, Aug 28, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,362
    George1776
    Sep 14, 2006
  4. Lance Wynn
    Replies:
    1
    Views:
    1,916
    Lance Wynn
    Feb 3, 2008
  5. yawnmoth
    Replies:
    97
    Views:
    4,800
    Bent C Dalager
    Feb 27, 2009
Loading...

Share This Page