# Nesting exception

**URL:** <https://www.ruby-forum.com/t/nesting-exception/146566>\
**Category:** Ruby\
**Created:** [August 22, 2008, 11:52am UTC](https://www.ruby-forum.com/t/nesting-exception/146566 "2008-08-22T11:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 22, 2008, 11:52am UTC](https://www.ruby-forum.com/t/nesting-exception/146566/1 "2008-08-22T11:52:00Z")

</div>

Hi  
I have the following code It is working I have no file named a  
begin  
f=File.open(‘a’)  
rescue Exception =\> exception  
if exception.message == ‘No such file or directory - a’  
puts ’ in first rescue’  
else  
puts ‘in else case’  
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  
f=File.open(‘a’)  
rescue Exception =\> exception  
if exception.message1 != ‘No such file or directory - a’  
puts ’ in first rescue’  
else  
puts ‘in else case’  
rescue NoMethodError =\> e:  
puts e.to\_s

# raise

# puts ‘error’

```
   end

```

end  
But it gives syntax error  
rescue NoMethodError =\> e:  
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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 22, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/nesting-exception/146566/2 "2008-08-22T12:11:21Z")

</div>

On Fri, 2008-08-22 at 18:48 +0900, Sijo Kg wrote:

> end  
> if exception.message1 != ‘No such file or directory - a’  
> rescue NoMethodError =\> e:  
> 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

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

rescue NoMethodError =\> e

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 22, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/nesting-exception/146566/3 "2008-08-22T12:33:00Z")

</div>

I tried  
begin  
f=File.open(‘a’)  
rescue Exception =\> exception  
if exception.message1 == ‘No such file or directory - a’  
puts ’ in if part of rescue’  
else  
puts ‘in else case’  
rescue NoMethodError =\> e:  
puts e.to\_s  
end  
end  
Above I purposefully changed exception.message to  
exception.message1. If it was exception.message and also  
rescue NoMethodError =\> e:  
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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 22, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/nesting-exception/146566/4 "2008-08-22T12:54:47Z")

</div>

2008/8/22 Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> end
> 
> ```
> 
> end  
> Above I purposefully changed exception.message to  
> exception.message1. If it was exception.message and also  
> rescue NoMethodError =\> e:  
> 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?)

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

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [August 22, 2008, 1:12pm UTC](https://www.ruby-forum.com/t/nesting-exception/146566/5 "2008-08-22T13:12:51Z")

</div>

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

Sijo
