Using inline "rescue" with error class

Discussion in 'Ruby' started by Iñaki Baz Castillo, Mar 3, 2009.

  1. Hi, instead of:

    =2D---------
    begin
    IPAddr.new(value)
    rescue ArgumentError
    puts "Invalid IP"
    end
    =2D---------

    I would like to just write:
    IPAddr.new(value) rescue ArgumentError puts "Invalid IP"

    Unfortunatelly it doesn't work when the rescue action occurs:
    =2D-----------
    Invalid IP
    NoMethodError: undefined method `ArgumentError' for main:Object
    =2D-----------

    Do I miss some way to set the error class when using inline rescue?

    Thanks a lot.

    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Mar 3, 2009
    #1
    1. Advertising

  2. Iñaki Baz Castillo

    lasitha Guest

    On Tue, Mar 3, 2009 at 5:58 AM, I=F1aki Baz Castillo <> wrote:
    > I would like to just write:
    > =A0IPAddr.new(value) rescue ArgumentError puts "Invalid IP"
    >
    > Unfortunatelly it doesn't work when the rescue action occurs:
    > ------------
    > Invalid IP
    > NoMethodError: undefined method `ArgumentError' for main:Object
    > ------------
    >


    The rescue statement modifier doesn't take an exception parameter.
    You'd have to make do with:
    IPAddr.new(value) rescue puts "Invalid IP"

    This of course makes the construct much less appealing.
    There has been a very recent thread related to this.

    Solidarity,
    lasitha
    lasitha, Mar 3, 2009
    #2
    1. Advertising

  3. lasitha wrote:
    > On Tue, Mar 3, 2009 at 5:58 AM, I�aki Baz Castillo <> wrote:
    >> I would like to just write:
    >> �IPAddr.new(value) rescue ArgumentError puts "Invalid IP"
    >>
    >> Unfortunatelly it doesn't work when the rescue action occurs:
    >> ------------
    >> Invalid IP
    >> NoMethodError: undefined method `ArgumentError' for main:Object
    >> ------------
    >>

    >
    > The rescue statement modifier doesn't take an exception parameter.
    > You'd have to make do with:
    > IPAddr.new(value) rescue puts "Invalid IP"


    ... which rescues StandardError and all subclasses.

    However you probably wouldn't want to do this:

    addr = IPAddr.new(value) rescue puts "Invalid IP"
    do_something_with(addr)

    because the second line would get addr equal to nil, and further errors
    would occur. One solution is just to let the ArgumentError propagate
    upwards, because the exception message already says "invalid address":

    irb(main):002:0> IPAddr.new("x")
    ArgumentError: invalid address

    Having said that, sometimes I do like to include the offending value in
    error messages, so I can end up writing stuff like

    begin
    addr = IPAddr.new(value)
    rescue ArgumentError
    raise ArgumentError, "Invalid IP address #{value.inspect}"
    end

    The risk here is that if the user provides an arbitrarily long string,
    you'll get an arbitrarily long exception message too.

    Another option is to stick rescue clause(s) at the end of a method body,
    because you don't need a 'begin'.

    def foo(value)
    addr = IPAddr.new(value)
    do_something_with(addr)
    rescue ArgumentError
    puts "Invalid IP address"
    raise # or not, it's up to you
    end

    On the plus side, this prevents the nil addr being used. On the minus
    side, *any* ArgumentError in the body of foo or any of the methods it
    calls will be reported as "Invalid IP address"

    Regards,

    Brian.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Mar 3, 2009
    #3
  4. El Martes, 3 de Marzo de 2009, Brian Candler escribi=C3=B3:
    > lasitha wrote:
    > > On Tue, Mar 3, 2009 at 5:58 AM, I=EF=BF=BDaki Baz Castillo <=

    et> wrote:
    > >> I would like to just write:
    > >> =EF=BF=BDIPAddr.new(value) rescue ArgumentError puts "Invalid IP"
    > >>
    > >> Unfortunatelly it doesn't work when the rescue action occurs:
    > >> ------------
    > >> Invalid IP
    > >> NoMethodError: undefined method `ArgumentError' for main:Object
    > >> ------------

    > >
    > > The rescue statement modifier doesn't take an exception parameter.
    > > You'd have to make do with:
    > > IPAddr.new(value) rescue puts "Invalid IP"

    >
    > ... which rescues StandardError and all subclasses.
    >
    > However you probably wouldn't want to do this:
    >
    > addr =3D IPAddr.new(value) rescue puts "Invalid IP"
    > do_something_with(addr)
    >
    > because the second line would get addr equal to nil, and further errors
    > would occur. One solution is just to let the ArgumentError propagate
    > upwards, because the exception message already says "invalid address":
    >
    > irb(main):002:0> IPAddr.new("x")
    > ArgumentError: invalid address
    >
    > Having said that, sometimes I do like to include the offending value in
    > error messages, so I can end up writing stuff like
    >
    > begin
    > addr =3D IPAddr.new(value)
    > rescue ArgumentError
    > raise ArgumentError, "Invalid IP address #{value.inspect}"
    > end
    >
    > The risk here is that if the user provides an arbitrarily long string,
    > you'll get an arbitrarily long exception message too.
    >
    > Another option is to stick rescue clause(s) at the end of a method body,
    > because you don't need a 'begin'.
    >
    > def foo(value)
    > addr =3D IPAddr.new(value)
    > do_something_with(addr)
    > rescue ArgumentError
    > puts "Invalid IP address"
    > raise # or not, it's up to you
    > end
    >
    > On the plus side, this prevents the nil addr being used. On the minus
    > side, *any* ArgumentError in the body of foo or any of the methods it
    > calls will be reported as "Invalid IP address"


    Thanks for so good explanation.


    =2D-=20
    I=C3=B1aki Baz Castillo
    Iñaki Baz Castillo, Mar 3, 2009
    #4
    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. Abhi
    Replies:
    2
    Views:
    734
    E. Robert Tisdale
    Jul 3, 2003
  2. W0rtex Indigo
    Replies:
    3
    Views:
    132
    W0rtex Indigo
    Jun 7, 2007
  3. Greg Hauptmann
    Replies:
    4
    Views:
    112
    Greg Hauptmann
    Jan 11, 2009
  4. Iñaki Baz Castillo

    Using "rescue" inline

    Iñaki Baz Castillo, Feb 25, 2009, in forum: Ruby
    Replies:
    8
    Views:
    142
    Iñaki Baz Castillo
    Feb 26, 2009
  5. Iñaki Baz Castillo

    Rub 1.9: "inline rescue" doesn't work?

    Iñaki Baz Castillo, Mar 1, 2009, in forum: Ruby
    Replies:
    10
    Views:
    213
    Gregory Brown
    Mar 2, 2009
Loading...

Share This Page