[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Using inline "rescue" with error class

Iñaki Baz Castillo

3/3/2009 12:29:00 AM

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

5 Answers

lasitha

3/3/2009 5:13:00 AM

0

On Tue, Mar 3, 2009 at 5:58 AM, I=F1aki Baz Castillo <ibc@aliax.net> 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

Brian Candler

3/3/2009 9:06:00 AM

0

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:

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-....

Iñaki Baz Castillo

3/3/2009 9:31:00 PM

0

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 <ibc@aliax.n=
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

Dave Peterson

2/2/2010 6:49:00 PM

0

Both of these userforms are open.
Userform1 contains the calendar control.
Notes contains the Textbox.

This is in the Userform1 module:

Option Explicit
Private Sub Calendar1_Click()
Notes.TextBox1.Value = Format(Me.Calendar1.Value, "mmmm dd, yyyy")
End Sub






NFL wrote:
>
> I'm working with 2 forms.
>
> Userform1 is 1st form where the calendar is already made and the other form
> is called NOTES:
>
> With NOTES opened, I would like to enter a date in the textbox by using a
> calendar from Userform1.
>
> Hope that makes sense...
>
> "Dave Peterson" wrote:
>
> > I'm not sure what you're doing and what code goes in what userform module...
> >
> > But I'd try this in Userform1.
> >
> > Option Explicit
> > Private Sub Calendar1_Click()
> > with ActiveCell
> > .numberformat = "mmmm dd, yyyy" 'unambiguous format for testing!
> > .value = me.Calendar1.Value
> > end with
> > End Sub
> >
> > Private Sub UserForm_Activate()
> > Me.Calendar1.Value = format(Date, "mmmm dd, yyyy") 'unambiguous format!
> > End Sub
> >
> >
> > But I don't understand what you're doing with the other stuff to guess.
> >
> > NFL wrote:
> > >
> > > The calendar is named USERFORM1 and here is the code for it.
> > >
> > > Option Explicit
> > >
> > > Private Sub Calendar1_Click()
> > >
> > > ActiveCell = Calendar1.Value
> > > ActiveCell.NumberFormat = "mm/dd/yy"
> > >
> > > End Sub
> > >
> > > Private Sub UserForm_Activate()
> > > Me.Calendar1.Value = Date
> > > End Sub
> > >
> > > I have another form called NOTES and what I like to do is when I select a
> > > textbox I would like enter a date by using the calendar. Here is the code I
> > > have in my textbox.
> > >
> > > I removed the 2nd entry because I would always get a 12/12/2003 date in the
> > > textbox. The calendar works great, but I can't select a date. Thank you for
> > > your help!
> > >
> > > Private Sub TextBox60_Change()
> > > UserForm1.Show
> > > 'Me.TextBox60 = [UserForm1].Calendar1.Value
> > >
> > > End Sub
> >
> > --
> >
> > Dave Peterson
> > .
> >

--

Dave Peterson

NFL

2/2/2010 7:28:00 PM

0

Thank you!

"Dave Peterson" wrote:

> Both of these userforms are open.
> Userform1 contains the calendar control.
> Notes contains the Textbox.
>
> This is in the Userform1 module:
>
> Option Explicit
> Private Sub Calendar1_Click()
> Notes.TextBox1.Value = Format(Me.Calendar1.Value, "mmmm dd, yyyy")
> End Sub
>
>
>
>
>
>
> NFL wrote:
> >
> > I'm working with 2 forms.
> >
> > Userform1 is 1st form where the calendar is already made and the other form
> > is called NOTES:
> >
> > With NOTES opened, I would like to enter a date in the textbox by using a
> > calendar from Userform1.
> >
> > Hope that makes sense...
> >
> > "Dave Peterson" wrote:
> >
> > > I'm not sure what you're doing and what code goes in what userform module...
> > >
> > > But I'd try this in Userform1.
> > >
> > > Option Explicit
> > > Private Sub Calendar1_Click()
> > > with ActiveCell
> > > .numberformat = "mmmm dd, yyyy" 'unambiguous format for testing!
> > > .value = me.Calendar1.Value
> > > end with
> > > End Sub
> > >
> > > Private Sub UserForm_Activate()
> > > Me.Calendar1.Value = format(Date, "mmmm dd, yyyy") 'unambiguous format!
> > > End Sub
> > >
> > >
> > > But I don't understand what you're doing with the other stuff to guess.
> > >
> > > NFL wrote:
> > > >
> > > > The calendar is named USERFORM1 and here is the code for it.
> > > >
> > > > Option Explicit
> > > >
> > > > Private Sub Calendar1_Click()
> > > >
> > > > ActiveCell = Calendar1.Value
> > > > ActiveCell.NumberFormat = "mm/dd/yy"
> > > >
> > > > End Sub
> > > >
> > > > Private Sub UserForm_Activate()
> > > > Me.Calendar1.Value = Date
> > > > End Sub
> > > >
> > > > I have another form called NOTES and what I like to do is when I select a
> > > > textbox I would like enter a date by using the calendar. Here is the code I
> > > > have in my textbox.
> > > >
> > > > I removed the 2nd entry because I would always get a 12/12/2003 date in the
> > > > textbox. The calendar works great, but I can't select a date. Thank you for
> > > > your help!
> > > >
> > > > Private Sub TextBox60_Change()
> > > > UserForm1.Show
> > > > 'Me.TextBox60 = [UserForm1].Calendar1.Value
> > > >
> > > > End Sub
> > >
> > > --
> > >
> > > Dave Peterson
> > > .
> > >
>
> --
>
> Dave Peterson
> .
>