Search code examples
rubysocketsrescue

Rescue ERRNO::EADDRINUSE in UDPSocket#bind


I'm trying to rescue the exception that ruby raises when you try to use the same address twice in a bind statement. The documentation is not very helpful.
Here's what I want:

require 'socket'
s = UDPSocket.new(Socket::AF_INET)
begin
  s.bind address,port
rescue #Address_in_use => e
  #code
end

Solution

  • rescue without an explicit Exception class only rescues StandardError and its subclasses. You should do the following:

    rescue Errno::EADDRINUSE => ex
      #code
    end