# Can't rescue Errno::EBADF

**URL:** <https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819>\
**Category:** Ruby\
**Created:** [September 5, 2007, 8:36am UTC](https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819 "2007-09-05T08:36:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [September 5, 2007, 8:36am UTC](https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819/1 "2007-09-05T08:36:27Z")

</div>

Hi,

I can’t seem to do a very simple thing: I cannot rescue a Errno::EBADF  
exception in my code. I know it is a Errno::EBADF exception, since I  
can rescue it through the generic exception type (Exception) and then  
I print the message. However, if I replace the rescue statement from  
“rescue Exception =\> e” to “rescue Errno::EBADF”, the code doesn’t go  
through the rescue block.

Am I doing something obvisouly wrong or is there something tricky  
about catching this type of exceptions?

I want to mention that I’m not using Rails or rake (all the posts that  
I’ve seen on Errno::EBADF are on these 2 subjects).

Thanks,  
Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [September 5, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819/2 "2007-09-05T16:29:00Z")

</div>

On 9/5/07, Mr\_Tibs [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> about catching this type of exceptions?
> 
> I want to mention that I’m not using Rails or rake (all the posts that  
> I’ve seen on Errno::EBADF are on these 2 subjects).
> 
> Thanks,  
> Tiberiu

Hi, could it be something else from your code? I’ve tried the  
following code and it catches the exception:

begin  
raise Errno::EBADF  
rescue Errno::EBADF  
puts “got it: #{$!}”  
end

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [September 6, 2007, 12:41am UTC](https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819/3 "2007-09-06T00:41:24Z")

</div>

Hi,

Thanks for the reply. I did try the same thing and I noticed it works.  
I really don’t know what I’m doing wrong in my code. Same thing  
happens when I try to catch Errno::ETIMEDOUT. Even when I make my own  
Exception class it does work as expected, but not for these 2  
exception types.

Tiberiu

---

<div class="post-metadata">

**Author:** ![mr\_tibs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/439d5e/32.png) [@mr\_tibs](https://www.ruby-forum.com/u/mr_tibs)\
**Post date:** [September 6, 2007, 3:34am UTC](https://www.ruby-forum.com/t/cant-rescue-errno-ebadf/113819/4 "2007-09-06T03:34:36Z")

</div>

Hi,

At Thu, 6 Sep 2007 07:40:07 +0900,  
Mr\_Tibs wrote in [ruby-talk:267813]:

> Thanks for the reply. I did try the same thing and I noticed it works.  
> I really don’t know what I’m doing wrong in my code. Same thing  
> happens when I try to catch Errno::ETIMEDOUT. Even when I make my own  
> Exception class it does work as expected, but not for these 2  
> exception types.

Does it work with SystemCallError?

With rescuing Exception, is the class of the exception really  
same as Errno::EBADF? try:

rescue Exception =\> e  
p e.class  
p e.class.ancestors  
p e.class == Errno::EBADF  
end
