# Ruby exception handling

**URL:** <https://www.ruby-forum.com/t/ruby-exception-handling/119698>\
**Category:** Ruby\
**Created:** [November 9, 2007, 10:29am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698 "2007-11-09T10:29:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 9, 2007, 10:29am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/1 "2007-11-09T10:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 9, 2007, 10:48am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/2 "2007-11-09T10:48:06Z")

</div>

Hello,

On Fri, Nov 09, 2007 at 06:29:40PM +0900, Franck Yu wrote:

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

---

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 9, 2007, 11:45am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/3 "2007-11-09T11:45:27Z")

</div>

On Fri, Nov 09, 2007 at 07:35:05PM +0900, Franck Yu wrote:

> Damien M. wrote:
> 
> > The body of a method definition can be considered as a begin/end block.  
> > So the code is OK.
> 
> It makes sense but where is it documented? In the Ruby programming  
> language spec? Where can I find the info?

I think it’s in the pickaxe but I can’t find it now. It’s in the spec:  
[http://www.headius.com/rubyspec/index.php/Methods](http://www.headius.com/rubyspec/index.php/Methods), the first line.

Regards,

---

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 9, 2007, 11:35am UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/4 "2007-11-09T11:35:05Z")

</div>

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

Damien M. wrote:

> Hello,
> 
> On Fri, Nov 09, 2007 at 06:29:40PM +0900, Franck Yu wrote:
> 
> > 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,

---

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 12, 2007, 4:11pm UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/5 "2007-11-12T16:11:05Z")

</div>

7stud – wrote:

> Franck Yu wrote:

Thank you very much for your help, Damien and 7stud!

---

<div class="post-metadata">

**Author:** ![franck](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/7ba0ec/32.png) [@franck](https://www.ruby-forum.com/u/franck)\
**Post date:** [November 9, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/ruby-exception-handling/119698/6 "2007-11-09T21:17:44Z")

</div>

Franck Yu wrote:

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