# Nesting exception

**URL:** <https://rubytalk.org/t/nesting-exception/48529>\
**Category:** ruby-talk\
**Created:** [22 August 2008 09:48 UTC](https://rubytalk.org/t/nesting-exception/48529 "2008-08-22T09:48:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sijo\_Kg](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sijo\_Kg](https://rubytalk.org/u/Sijo_Kg)\
**Post date:** [22 August 2008 09:48 UTC](https://rubytalk.org/t/nesting-exception/48529/1 "2008-08-22T09:48:09Z")

</div>

Hi  
&nbsp;&nbsp;&nbsp;I have the following code It is working I have no file named a  
begin  
&nbsp;&nbsp;f=File.open('a')  
&nbsp;&nbsp;rescue Exception =\> exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message == 'No such file or directory - a'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in first rescue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

So here I get 'in first rescue'

Now what i did is I deliberately changed exception.message1 .So sure I  
will get an excption here And How cn I handle that(This is for learning  
only).I tried like

begin  
&nbsp;&nbsp;f=File.open('a')  
&nbsp;&nbsp;rescue Exception =\> exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message1 != 'No such file or directory - a'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in first rescue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts e.to\_s  
&nbsp;&nbsp;&nbsp;# raise  
&nbsp;&nbsp;&nbsp;# puts 'error'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;But it gives syntax error  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
&nbsp;&nbsp;&nbsp;How can I solve this..Even now searching a lot in google I am not  
that much confident in exception handling in Ruby and also rails

Thanks in advance  
Sijo

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [22 August 2008 10:07 UTC](https://rubytalk.org/t/nesting-exception/48529/2 "2008-08-22T10:07:05Z")

</div>

Ignoring, whatever you are trying to do, your exception handling is  
wrong, it should be:

rescue NoMethodError =\> e

> **···**
>
> On Fri, 2008-08-22 at 18:48 +0900, Sijo Kg wrote:
> 
> > Hi  
> > &nbsp;&nbsp;&nbsp;I have the following code It is working I have no file named a  
> > begin  
> > &nbsp;&nbsp;f=File.open('a')  
> > &nbsp;&nbsp;rescue Exception =\> exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message == 'No such file or directory - a'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in first rescue'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > So here I get 'in first rescue'
> > 
> > Now what i did is I deliberately changed exception.message1 .So sure I  
> > will get an excption here And How cn I handle that(This is for learning  
> > only).I tried like
> > 
> > begin  
> > &nbsp;&nbsp;f=File.open('a')  
> > &nbsp;&nbsp;rescue Exception =\> exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message1 != 'No such file or directory - a'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in first rescue'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts e.to\_s  
> > &nbsp;&nbsp;&nbsp;# raise  
> > &nbsp;&nbsp;&nbsp;# puts 'error'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;But it gives syntax error  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
> > &nbsp;&nbsp;&nbsp;How can I solve this..Even now searching a lot in google I am not  
> > that much confident in exception handling in Ruby and also rails

---

<div class="post-metadata">

**Author:** ![Sijo\_Kg](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sijo\_Kg](https://rubytalk.org/u/Sijo_Kg)\
**Post date:** [22 August 2008 10:29 UTC](https://rubytalk.org/t/nesting-exception/48529/3 "2008-08-22T10:29:09Z")

</div>

I tried  
begin  
&nbsp;&nbsp;f=File.open('a')  
&nbsp;&nbsp;rescue Exception =\> exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message1 == 'No such file or directory - a'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in if part of rescue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts e.to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Above I purposefully changed exception.message to  
exception.message1. If it was exception.message and also  
&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
&nbsp;&nbsp;&nbsp;&nbsp;puts e.to\_s  
were absent I would get 'in if part of rescue'..So as everyone know  
exception.message1 doesnot exists. I would like to know how to handle  
that exception. Where to place that rescue(Am I right?)

Thanks  
Sijo

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [22 August 2008 10:50 UTC](https://rubytalk.org/t/nesting-exception/48529/4 "2008-08-22T10:50:15Z")

</div>

First of all, you cannot use a symbol where you are using it as Hemant  
already remarked.

Second, if you want to catch exceptions that occur during exception  
handling you need to put a begin rescue end block inside the rescue  
block.

robert

> **···**
>
> 2008/8/22 Sijo Kg \<sijo@maxxion.com\>:
> 
> > I tried  
> > begin  
> > f=File.open('a')  
> > rescue Exception =\> exception  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message1 == 'No such file or directory - a'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in if part of rescue'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts e.to\_s  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Above I purposefully changed exception.message to  
> > exception.message1. If it was exception.message and also  
> > &nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
> > &nbsp;&nbsp;&nbsp;puts e.to\_s  
> > were absent I would get 'in if part of rescue'..So as everyone know  
> > exception.message1 doesnot exists. I would like to know how to handle  
> > that exception. Where to place that rescue(Am I right?)
> 
> --  
> use.inject do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Sijo\_Kg](https://avatars.discourse-cdn.com/v4/letter/s/bbe5ce/32.png) [@Sijo\_Kg](https://rubytalk.org/u/Sijo_Kg)\
**Post date:** [22 August 2008 11:09 UTC](https://rubytalk.org/t/nesting-exception/48529/5 "2008-08-22T11:09:05Z")

</div>

Hi  
&nbsp;&nbsp;&nbsp;Thanks for the reply .The following is working and prints 'Also this  
error caught'  
begin  
&nbsp;&nbsp;f=File.open('a')  
&nbsp;&nbsp;rescue Exception =\> exception  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if exception.message1 == 'No such file or directory - a'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts ' in if part of rescue'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'in else case'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue NoMethodError =\> e:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Also this error caught'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end

Sijo

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
