# Rescu all errors and then case statement

**URL:** <https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614>\
**Category:** Ruby\
**Created:** [January 10, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614 "2008-01-10T17:17:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/1 "2008-01-10T17:17:11Z")

</div>

How can I read what type of error occured in rescue clause.  
For example, I have the following:

begin  
“some code here”

rescue  
case  
when “error type 1”

when “error type 2”  
end

end

I know I could raise errors within code section and then have specif  
rescues, but I want to have one rescue and then do different things with  
it in the rescue.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/2 "2008-01-10T17:23:21Z")

</div>

On Jan 10, 2008 2:17 PM, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when “error type 2”  
> end
> 
> end
> 
> I know I could raise errors within code section and then have specif  
> rescues, but I want to have one rescue and then do different things with  
> it in the rescue.
> 
> Thanks in advance.

You can do something like this:

begin  
…  
rescue =\> e  
  
end

basically, the “=\> e” bit will store the thrown error in the “e”  
variable.

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 7:12pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/3 "2008-01-10T19:12:11Z")

</div>

that works good. In addition to that is there a way to tell what error  
type is it ?

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 9:17pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/4 "2008-01-10T21:17:16Z")

</div>

e.class

Dan

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/5 "2008-01-10T21:37:25Z")

</div>

Daniel F. wrote:

> e.class
> 
> Dan  
> Thanks guys.

---

<div class="post-metadata">

**Author:** ![nicpon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/4491bb/32.png) [@nicpon](https://www.ruby-forum.com/u/nicpon)\
**Post date:** [January 10, 2008, 9:12pm UTC](https://www.ruby-forum.com/t/rescu-all-errors-and-then-case-statement/125614/6 "2008-01-10T21:12:36Z")

</div>

On Jan 10, 2008 5:17 PM, Michal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when “error type 2”  
> end
> 
> end
> 
> I know I could raise errors within code section and then have specif  
> rescues, but I want to have one rescue and then do different things with  
> it in the rescue.

errors = [IOError, ZeroDivisionError, ArgumentError]

errors.each do |klass|  
begin  
puts “raising #{klass.name}”  
raise klass  
rescue =\> e  
case e  
when IOError  
puts “it’s IOError!”  
when ZeroDivisionError  
puts “it’s ZeroDivisionError”  
else  
puts “other error: #{e.class.name}”  
end  
end  
end

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog
