ruby exception handling

F

Franck Yu

Hi,
I am creating a web site using Ruby on Rails and Paypal NVP API. I see
in Paypal SDK something like:

def nameOfaMethod
...
rescue
...
end

and I just want to make sure if it is correct. Usually rescue clause is
in a begin ... end block. Can def replace begin as begin has an end and
represents a block as well. Thanks!

Franck
 
D

Damien Merenne

Hello,

Hi,
I am creating a web site using Ruby on Rails and Paypal NVP API. I see
in Paypal SDK something like:

def nameOfaMethod
...
rescue
...
end

and I just want to make sure if it is correct. Usually rescue clause is
in a begin ... end block. Can def replace begin as begin has an end and
represents a block as well. Thanks!

The body of a method definition can be considered as a begin/end block.
So the code is OK.

Regards,
 
F

Franck Yu

Hi Damien,

It makes sense but where is it documented? In the Ruby programming
language spec? Where can I find the info?
Thank you for your answer!


Franck
 
7

7stud --

Franck said:
It makes sense but where is it documented? In the Ruby programming
language spec? Where can I find the info?

In pickaxe2, Chapter 22: The Ruby Language, section Method Definition,
p. 345, the listing for the general syntax of a method definition does
not indicate that a rescue block can be attached to a method. However,
on p. 347 the text says,

"The body of a method acts as if it were a begin/end block, in that it
may contain exception handling statements(rescue, else, and ensure)."

Also, in the same chapter, section Exceptions, p. 361, it says,

"A block or method may have multiple rescue clauses....When an exception
is raised, Ruby scans up the call stack until it finds an enclosing
begin/end block, method body, or statement with a rescue modifier."
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,698
Latest member
LydiaHalle

Latest Threads

Top