# Using inline "rescue" with error class

**URL:** <https://rubytalk.org/t/using-inline-rescue-with-error-class/52089>\
**Category:** ruby-talk\
**Created:** [3 March 2009 00:28 UTC](https://rubytalk.org/t/using-inline-rescue-with-error-class/52089 "2009-03-03T00:28:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [3 March 2009 00:28 UTC](https://rubytalk.org/t/using-inline-rescue-with-error-class/52089/1 "2009-03-03T00:28:59Z")

</div>

Hi, instead of:

> **···**
>
> ----------  
> begin  
> &nbsp;&nbsp;IPAddr.new(value)  
> rescue ArgumentError  
> &nbsp;&nbsp;puts "Invalid IP"  
> end  
> ----------
> 
> I would like to just write:  
> &nbsp;&nbsp;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  
> ------------
> 
> Do I miss some way to set the error class when using inline rescue?
> 
> Thanks a lot.
> 
> --  
> Iñaki Baz Castillo

---

<div class="post-metadata">

**Author:** ![lasitha](https://avatars.discourse-cdn.com/v4/letter/l/4af34b/32.png) [@lasitha](https://rubytalk.org/u/lasitha)\
**Post date:** [3 March 2009 05:13 UTC](https://rubytalk.org/t/using-inline-rescue-with-error-class/52089/2 "2009-03-03T05:13:21Z")

</div>

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

> **···**
>
> On Tue, Mar 3, 2009 at 5:58 AM, Iñaki Baz Castillo \<ibc@aliax.net\> 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  
> > ------------

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [3 March 2009 09:05 UTC](https://rubytalk.org/t/using-inline-rescue-with-error-class/52089/3 "2009-03-03T09:05:46Z")

</div>

lasitha 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:

&nbsp;&nbsp;addr = IPAddr.new(value) rescue puts "Invalid IP"  
&nbsp;&nbsp;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

&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;addr = IPAddr.new(value)  
&nbsp;&nbsp;rescue ArgumentError  
&nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError, "Invalid IP address #{value.inspect}"  
&nbsp;&nbsp;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'.

&nbsp;&nbsp;def foo(value)  
&nbsp;&nbsp;&nbsp;&nbsp;addr = IPAddr.new(value)  
&nbsp;&nbsp;&nbsp;&nbsp;do\_something\_with(addr)  
&nbsp;&nbsp;rescue ArgumentError  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Invalid IP address"  
&nbsp;&nbsp;&nbsp;&nbsp;raise # or not, it's up to you  
&nbsp;&nbsp;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.

> **···**
>
> > On Tue, Mar 3, 2009 at 5:58 AM, I�aki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [3 March 2009 21:31 UTC](https://rubytalk.org/t/using-inline-rescue-with-error-class/52089/4 "2009-03-03T21:31:14Z")

</div>

Thanks for so good explanation.

> **···**
>
> El Martes, 3 de Marzo de 2009, Brian Candler escribió:
> 
> > lasitha wrote:  
> > \> On Tue, Mar 3, 2009 at 5:58 AM, I�aki Baz Castillo \<ibc@aliax.net\> 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:
> > 
> > &nbsp;&nbsp;addr = IPAddr.new(value) rescue puts "Invalid IP"  
> > &nbsp;&nbsp;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
> > 
> > &nbsp;&nbsp;begin  
> > &nbsp;&nbsp;&nbsp;&nbsp;addr = IPAddr.new(value)  
> > &nbsp;&nbsp;rescue ArgumentError  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise ArgumentError, "Invalid IP address #{value.inspect}"  
> > &nbsp;&nbsp;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'.
> > 
> > &nbsp;&nbsp;def foo(value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;addr = IPAddr.new(value)  
> > &nbsp;&nbsp;&nbsp;&nbsp;do\_something\_with(addr)  
> > &nbsp;&nbsp;rescue ArgumentError  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "Invalid IP address"  
> > &nbsp;&nbsp;&nbsp;&nbsp;raise # or not, it's up to you  
> > &nbsp;&nbsp;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"
> 
> --  
> Iñaki Baz Castillo
